Javascript 由于未终止的字符串常量错误,添加行无效

Javascript 由于未终止的字符串常量错误,添加行无效,javascript,jquery,Javascript,Jquery,如何在表中添加以下行 我犯了错误 具有以下代码的未终止字符串常量 $('#myTable tbody')。追加(' ' ' ) 你需要在每一行的末尾放一个\来避免换行,也需要在单引号中放一个\'而不是' JavaScript希望字符串在它开始的同一行中终止-这就是为什么它说“unterminated string…”。要让它继续下去,你必须避开断线。这就是反斜杠的作用 此代码将在以下情况下工作: $('#myTable tbody').append( ' <td style=

如何在表中添加以下行

我犯了错误

具有以下代码的未终止字符串常量


$('#myTable tbody')。追加('
' 

' )
你需要在每一行的末尾放一个
\
来避免换行,也需要在单引号中放一个
\'
而不是
'


JavaScript希望字符串在它开始的同一行中终止-这就是为什么它说“unterminated string…”。要让它继续下去,你必须避开断线。这就是反斜杠的作用

此代码将在以下情况下工作:

$('#myTable tbody').append(
    ' <td style="width: 250px;">\
                <select id="List" class="FixedWidth" onclick="this.style.width=\'auto\';" onchange="this.blur();" onblur="this.style.width=\'250px\';" style="display: none;">\
                <div id="labelDiv"><label id="ComboBoxText"></label></div>\
                <input id="TID" name="TID" type="hidden" value="0">\
            </td>\
            <td class="Cell KeyCell" style="display: none;">\
                <input class="KeyInput" id="Key" maxlength="5" name="Key" placeholder="Hours" type="text" value="" style="color: rgb(169, 169, 169);"><br>\
                <textarea maxlength="2000" class="NameInput" cols="20" id="Name" name="Name" placeholder="enter here" rows="2" style="color: rgb(169, 169, 169);"></textarea>\
                <input id="ID" name="ID" type="hidden" value="0">\
            </td>'
    );
$('#myTable tbody')。追加(
' \
\
\
\
\
\

\ \ \ ' );
您需要使用
\

<table id="myTable">
   <tbody>
      <tr>
          <td></td><td></td>
      </tr>
   <tbody>
</table>

$('#myTable tbody').append(
    ' <td style="width: 250px;">
                <select id="List" class="FixedWidth" onclick="this.style.width=\'auto\';" onchange="this.blur();" onblur="this.style.width=\'250px\';" style="display: none;">
                <div id="labelDiv"><label id="ComboBoxText"></label></div>
                <input id="TID" name="TID" type="hidden" value="0">
            </td>
            <td class="Cell KeyCell" style="display: none;">
                <input class="KeyInput" id="Key" maxlength="5" name="Key" placeholder="Hours" type="text" value="" style="color: rgb(169, 169, 169);"><br>
                <textarea maxlength="2000" class="NameInput" cols="20" id="Name" name="Name" placeholder="enter here" rows="2" style="color: rgb(169, 169, 169);"></textarea>
                <input id="ID" name="ID" type="hidden" value="0">
            </td>'
    )

$('#myTable tbody')。追加(
' 

' )
您的代码。字符串连接的概念

 '<td style="width: 250px;">'+
       '<select id="List" class="FixedWidth" onclick="this.style.width=\'auto\'" onchange="this.blur()" onblur="this.style.width=\'250px\'" sty"display: none;">'+
               ' <div id="labelDiv"><label id="ComboBoxText"></label></div>'+
                '<input id="TID" name="TID" type="hidden" value="0">'+
            '</td>'+
            '<td class="Cell KeyCell" style="display: none;">'+
               ' <input class="KeyInput" id="Key" maxlength="5" name="Key" placeholder="Hours" type="text" value="" style="color: rgb(169, 169, 169);"><br>'+
 '<textarea maxlength="2000" class="NameInput" cols="20" id="Name" name="Name" placeholder="enter here" rows="2" style="color: rgb(169, 169, 169);"></textarea>'+
                '<input id="ID" name="ID" type="hidden" value="0">'+
            '</td>'
“”+
''+
' '+
''+
''+
''+
“
”+ ''+ ''+ ''
您必须将所有这些
html
放在一行,或者
使用
concatation
操作
+
将这些巨大的字符串断开,如下所示

$('#myTable tbody').append(

    ' <td style="width: 250px;">'+
                '<select id="List" class="FixedWidth" onclick="this.style.width=\'auto\'" onchange="this.blur()" onblur="this.style.width=\'250px\'" style="display: none">'+
                '<div id="labelDiv"><label id="ComboBoxText"></label></div>'+
                '<input id="TID" name="TID" type="hidden" value="0">'+
            '</td><td class="Cell KeyCell" style="display: none">'+
                '<input class="KeyInput" id="Key" maxlength="5" name="Key"'+ 'placeholder="Hours" type="text" value="" style="color: rgb(169, 169, 169);"><br>'+
                '<textarea maxlength="2000" class="NameInput" cols="20" id="Name" name="Name" placeholder="enter here" rows="2" style="color: rgb(169, 169, 169);"></textarea>'+
                '<input id="ID" name="ID" type="hidden" value="0"></td>'
    );
$('#myTable tbody')。追加(
' '+
''+
''+
''+
''+
“
”+ ''+ '' );
代码的问题在于您没有使用字符串连接以及您给出这种连接的方式。style.width=auto;这也是错误的。我希望这有助于:

$('#myTable tbody').append(
     ' <td style="width: 250px;">'+
       '<select id="List" class="FixedWidth" onclick="this.style.width=auto;" onchange="this.blur();" onblur="this.style.width=250px;" sty"display: none;">'+
               ' <div id="labelDiv"><label id="ComboBoxText"></label></div>'+
                '<input id="TID" name="TID" type="hidden" value="0">'+
            '</td>'+
            '<td class="Cell KeyCell" style="display: none;">'+
               ' <input class="KeyInput" id="Key" maxlength="5" name="Key" placeholder="Hours" type="text" value="" style="color: rgb(169, 169, 169);"><br>'+
 '<textarea maxlength="2000" class="NameInput" cols="20" id="Name" name="Name" placeholder="enter here" rows="2" style="color: rgb(169, 169, 169);"></textarea>'+
                '<input id="ID" name="ID" type="hidden" value="0">'+
            '</td>'

    )
$('#myTable tbody')。追加(
' '+
''+
' '+
''+
''+
''+
“
”+ ''+ ''+ '' )

演示:

你需要避开你的单一报价看看你在这里发布的代码。语法突出显示应该会跳出来。你把你的单引号和双引号混在一起了。我不明白你的反对票。这是本页上唯一完整的答案。真的吗?:)我写这篇评论的时候,看看上面的文章你的回答是“现在”正确。我希望你能多获得3票,超过Pranavs的答案,JavaScript希望字符串在开始的同一行终止。要让它继续下去,你必须避开断线。这就是反斜杠的作用。
$('#myTable tbody').append(
     ' <td style="width: 250px;">'+
       '<select id="List" class="FixedWidth" onclick="this.style.width=auto;" onchange="this.blur();" onblur="this.style.width=250px;" sty"display: none;">'+
               ' <div id="labelDiv"><label id="ComboBoxText"></label></div>'+
                '<input id="TID" name="TID" type="hidden" value="0">'+
            '</td>'+
            '<td class="Cell KeyCell" style="display: none;">'+
               ' <input class="KeyInput" id="Key" maxlength="5" name="Key" placeholder="Hours" type="text" value="" style="color: rgb(169, 169, 169);"><br>'+
 '<textarea maxlength="2000" class="NameInput" cols="20" id="Name" name="Name" placeholder="enter here" rows="2" style="color: rgb(169, 169, 169);"></textarea>'+
                '<input id="ID" name="ID" type="hidden" value="0">'+
            '</td>'

    )