2016-10-19 18 views
0

动态地将dropdownlist添加到表中可以使用jquery append将dropdownlist添加到表中。使用append()

例如,

$(#table).append("<tr><td>@Html.DropDownList('TP',new SelectList(@Model.RefList, 'Value', 'Text',@Model.Ref))</td></tr>"); 

我不知道如何将此“@ Html.Dropdownlist”更改为有效的字符串。

+0

只要检查'Html.DropDownList()'方法生成的html并复制它。但是,将它包含在隐藏元素的视图中可能更容易,然后克隆它并追加该文本。 –

+0

将剃刀更改为html –

回答

0

你不能用JavaScript添加服务器控件,您可以添加一个HTML选择和使用加载选项ajax

$('#table').append('<select id="mySelect"></select>'); 

例子:

$.ajax({ 
    url: "myServiceURL" 
}).done(function(myOptions) { 

    $.each(myOptions, function(key, value) { 

     $('#mySelect') 
      .append($("<option></option>") 
       .attr("value",key) 
       .text(value)); 
    }); 
}); 
0

此代码将追加选择菜单到最后桌子的一排。

var select_list = '<select id="list">'; 
//you can add more options by repeating the next line & change text,value 
select_list += '<option value="changevalue">change text</option>'; 
select_list += '</select>'; 
$("#table").append("<tr><td>"+select_list+"</td></tr>");