0

I have this javascript code:

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);

This is the rendered code:

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&#39;s broken today!</option>
<option value="179">Get a physical &#39;high&#39; today!</option>
<option value="150">It&#39;s OK to be unassertive sometimes!</option>
<option value="169">Just &#39;let it wash&#39; 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&#39;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);

It works just fine on firefox, but on chrome I'm getting this error on the first line:

Uncaught SyntaxError: Unexpected identifier

Any clues?

4

3 に答える 3

1

Don't concatenate:

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);

Also this seems like an awful way to mix client side with server side scripting. Why don't you simply build the whole markup directly on the server?

于 2012-07-25T12:31:02.740 に答える
0
'<tr id="newdo" class="odd"><td>' + <select 

is the problem. you're not generating a string but trying to concatenate string with html. So Darin's right - don't concatenate or concatenate with the inner html of the dropdown&co rendered somewhere else ("display: none" div).

于 2012-07-25T13:01:24.323 に答える
0

Old one but found a different solution to a similar problem.

To get an HTML out of the object generated by:

 @Html.DropDownList("does", new SelectList(Model.Does, "Id", "Name"),new { @class = "newDoSelect" })

you can use ToHtmlString() method, like that:

 @Html.DropDownList("does", new SelectList(Model.Does, "Id", "Name"),new { @class = "newDoSelect" }).ToHtmlString()
于 2016-09-01T19:16:49.190 に答える