简体   繁体   English

Javascript中意外的标识符用于循环

[英]Unexpected identifier in Javascript for loop

This seems to work 75% of the time and the modal function executes with the parameters available, but every few buttons in the table I'll get a Uncaught SyntaxError: Unexpected identifier. 这似乎可以工作75%的时间,并且使用可用参数执行模态函数,但是表中的每几个按钮我都会得到Uncaught SyntaxError:意外标识符。 Does this have to do with inproper closure? 这与关闭不当有关吗? My google searches landed on that as a potential issue but I was unable to implement any of the solutions into my current method here. 我的Google搜索认为这是一个潜在的问题,但我无法在此处将任何解决方案实施到当前方法中。

html += '<thead><th>Question</th><th>Answer 1</th><th>Answer 2</th><th>Answer 3</th><th>Answer 4</th></thead>';

        for (var i = 0; i < questions.length; i++) {

            question = questions[i].question;
            questionTitle = question.q;
            answer1title = question.a1;
            answer2title = question.a2;

            html += '<tr><td class="question"><b>'
             + question.q
             + '</b></td><td class="answer1">'
             + question.a1
             + '</td><td class="answer2">'
             + question.a2
             + '</td><td class="answer3">'
             + question.a3
             + '</td><td class="answer4">'
             + question.a4
             + '</td><td class="edit">'
             + '<button onclick="openQuestionModal(\''+questionTitle+'\', \''+answer1title+'\', \''+answer2title+'\')" class="btn btn-small btn-primary" id="questionEdit" type="button">Edit</button>'
             + '</td></tr>';   

        }

        $('#questionsTable').append(html); 

The problem is that any of questionTitle , answer1title , or answer2title may have a single quote in their values, which breaks the string concatenation you are attempting. 问题在于, questionTitleanswer1titleanswer2title任何一个answer2title可能在其值中包含单引号,这会破坏您尝试的字符串连接。 You need to replace all occurrences of a single quote with an escaped single quote. 您需要用转义的单引号替换所有出现的单引号。 So something like this: 所以像这样:

http://jsfiddle.net/ZYnfc/2/ http://jsfiddle.net/ZYnfc/2/

$(document).ready(function () {
    var par = "I'm the problem";
    var replacer = new RegExp("'", "g");
    var new_btn = $('<button onclick="clicker(\'' + par.replace(replacer, "\\'") + '\');">ASDF</button>');
    $("#main").append(new_btn);
});

function clicker(param) {
    alert(param);
}

You'll have to take my example and apply it to your actual code, but the main point is the use of the replace method and the replacer regex for each of your variables you're concatenating in the function call. 您必须以我的示例为例,并将其应用于您的实际代码,但要点是,对于要在函数调用中串联的每个变量,都使用replace方法和replacer regex。

There are better ways to append HTML to an area, especially when it comes to a table. 有更好的方法可以将HTML附加到区域,尤其是在涉及表格时。 And I would suggest binding events with jQuery, not inline. 我建议使用jQuery而不是内联绑定事件。 That's a different topic and is unrelated to your problem, although it could've helped avoid it in the first place. 这是一个不同的主题,并且与您的问题无关,尽管它可以从一开始就避免它。

May be you should escape the string before adding to onclick attribute: 可能是您应该在添加到onclick属性之前对字符串进行转义:

+ '<button onclick="openQuestionModal(\''+ escape(questionTitle)+'\', \''+ escape(answer1title)+'\', \''+escape(answer2title)+'\')" class="btn btn-small btn-primary" id="questionEdit" type="button">Edit</button>'

And Use : 和使用:

unescape('str_to_revert')// to get the data back inside your function.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM