我有这个javascript代码:
var markup = '<tr id="newdo" class="odd"><td>' + @Html.DropDownList("does", new SelectList(Model.Does, "Id", "Name"),new { @class = "newDoSelect" }) + '</td><td class="small">'+ @Html.DropDownList("restriction", Model.Restrictions) + '</td><td>' + @Html.ListBox("doobjectives", new MultiSelectList(Model.AllObjectives, "Id", "Name"), new { Multiple = "multiple", @class="doobjectives" }) + '</td><td></td></tr>';
$('#doList tr:last').after(markup);
这是呈现的代码:
var markup = '<tr id="newdo" class="odd"><td>' + <select class="newDoSelect" id="does_3_" name="does[3]"><option value="319">A breath of fresh air..</option>
<option value="176">Fix something that's broken today!</option>
<option value="179">Get a physical 'high' today!</option>
<option value="150">It's OK to be unassertive sometimes!</option>
<option value="169">Just 'let it wash' today.</option>
<option value="200">WOW!</option>
<option value="233">Write something for 15 minutes.</option>
<option value="104">You CAN be individually-centred: try it today.</option>
<option value="111">You can be livelier: try this.</option>
<option value="77">You can be more definite in how you are.</option>
<option value="58">You don't always have to be unassertive!</option>
</select> + '</td><td class="small">'+ <select id="restriction_3_" name="restriction[3]"><option value="negative">negative</option>
<option value="neutral">neutral</option>
<option value="positive">positive</option>
</select> + '</td><td>' + <select Multiple="multiple" class="doobjectives" id="doobjectives_3_" multiple="multiple" name="doobjectives[3]"><option value="4">another test</option>
<option value="24">testing objective 10</option>
<option value="25">testing objective 11</option>
<option value="13">testing objective 2</option>
<option value="14">testing objective 3</option>
<option value="15">testing objective 4</option>
<option value="16">testing objective 5</option>
<option value="17">testing objective 7</option>
<option value="19">testing objective 8</option>
<option value="18">testing objective 9</option>
</select> + '</td><td></td></tr>';
$('#doList tr:last').after(markup);
它在firefox上运行得很好,但在chrome上我在第一行收到此错误:
未捕获的SyntaxError:意外的标识符
任何线索?
答案 0 :(得分:1)
不要连接:
var markup = '<tr id="newdo" class="odd"><td>@Html.DropDownList("does", new SelectList(Model.Does, "Id", "Name"),new { @class = "newDoSelect" })</td><td class="small">@Html.DropDownList("restriction", Model.Restrictions)</td><td>@Html.ListBox("doobjectives", new MultiSelectList(Model.AllObjectives, "Id", "Name"), new { Multiple = "multiple", @class="doobjectives" })</td><td></td></tr>';
$('#doList tr:last').after(markup);
此外,这似乎是将客户端与服务器端脚本混合的糟糕方式。为什么不直接在服务器上构建整个标记?
答案 1 :(得分:0)
'<tr id="newdo" class="odd"><td>' + <select
是问题所在。你没有生成一个字符串,但试图用html连接字符串。所以Darin的权利 - 不要与下拉列表的内部html连接或连接,并在其他地方呈现(“display:none”div)。
答案 2 :(得分:0)
旧的,但找到了解决类似问题的不同解决方案。
从以下内容生成的对象中获取HTML:
@Html.DropDownList("does", new SelectList(Model.Does, "Id", "Name"),new { @class = "newDoSelect" })
您可以使用ToHtmlString()
方法,例如:
@Html.DropDownList("does", new SelectList(Model.Does, "Id", "Name"),new { @class = "newDoSelect" }).ToHtmlString()