最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught SyntaxError: Unexpected identifier with .append() string - Stack Overflow

programmeradmin4浏览0评论

I keep getting this error in the $('#savetickets-list') line. I want to dynamically add fields to a table, the table has the id in HTML.

<div class="savetickets-list">
</div>

In javascript I fill the table in a for loop

for (var i = 0; i < len; i++) {
    // the data es from a web database
    var ticketname = results.rows.item(i).iTicketName;

    $('#savetickets-list').append('
        <div class="saveticket gradient-top">
            <h3>' + ticketname + '</h3>
        </div>
    ');
}   

I dont know how to solve this. jQuery is loaded, I also checked the name of the selector.

Please help.

I keep getting this error in the $('#savetickets-list') line. I want to dynamically add fields to a table, the table has the id in HTML.

<div class="savetickets-list">
</div>

In javascript I fill the table in a for loop

for (var i = 0; i < len; i++) {
    // the data es from a web database
    var ticketname = results.rows.item(i).iTicketName;

    $('#savetickets-list').append('
        <div class="saveticket gradient-top">
            <h3>' + ticketname + '</h3>
        </div>
    ');
}   

I dont know how to solve this. jQuery is loaded, I also checked the name of the selector.

Please help.

Share edited Jun 28, 2012 at 7:57 Esailija 140k23 gold badges279 silver badges328 bronze badges asked Jun 28, 2012 at 7:51 最白目最白目 3,6549 gold badges64 silver badges116 bronze badges 4
  • 1 What is the value of ticketname? Can you post the contents of results.rows in your question. – Rory McCrossan Commented Jun 28, 2012 at 7:54
  • 1 This looks for an id: $('#savetickets-list'), while you have class="savetickets-list" in your markup. – Jared Farrish Commented Jun 28, 2012 at 7:55
  • I have edited the title so it's more obvious that the code doesn't even run in the first place (Syntax Error, cmon people..) – Esailija Commented Jun 28, 2012 at 7:58
  • @Rory McCrossan: It also didnt work withouth the "ticketname" in the .append('..content..') content. So I think it´s ok if I save the work of posting it, but thanks anyway – 最白目 Commented Jun 28, 2012 at 8:00
Add a ment  | 

3 Answers 3

Reset to default 5
$('#savetickets-list').append('\
        <div class="saveticket gradient-top">\
            <h3>' + ticketname + '</h3>\
        </div>\
    ');

when you want to write multiline strings in JS, you must escape new lines.

It is because you are using new lines.

JS does not automatically read new lines for you. It treats them as new statements.

The way I prefer to do this is like:

$('#savetickets-list').append('<div class="saveticket gradient-top">'+
    '<h3>' + ticketname + '</h3>'+
'</div>');

Just checked. The problem is in the newlines, you have to concatenate strings or put all the statement in a single line.

发布评论

评论列表(0)

  1. 暂无评论