![](/img/trans.png)
[英]How can I use jquery to set the selected value of aspnet drop down list
[英]How can I set the value of a List box using jQuery?
嗨,我有一個由web方法返回的列表。 這需要設置為Listbox嗎? 如何才能做到這一點 ? 提前致謝
我使用此代碼設置了2個標簽的值。 現在還需要設置一個列表框。
$(document).ready(function() {
$.ajax({
type: "POST",
url: "WebForm2.aspx/GetTime1",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
$('#<%=Label2.ClientID %>').html(result.d.Label1);
$('#<%=Label3.ClientID %>').html(result.d.Label2);
}
});
});
這是webmethod返回的這個類的對象
public class StatusViewModel
{
public string Label1 { get; set; }
public string Label2 { get; set; }
public List<string> ListBox { get; set; }
}
嘗試返回列表,然后將列表綁定到列表框。
$.each(ListBox, function(index, item) {
$("#ListBoxtoBeFilled").get(0).options[$("#ListBoxtoBeFilled").get(0).options.length] = new Option(item);
});
如果我理解正確,ListBox已經在頁面,讓它有服務器ID lbYourListBox
for (var i = 0; i < result.ListBox.length; i++) {
$(document.createElement("option")).attr("value",result.ListBox[i]).html(result.ListBox[i])
.appendTo('#<%=lbYourListBox.ClientID %>')}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.