How can I set the value of a List box using jQuery?
Hi I have a List which is returned by a webmethod. This needs to be set to a Listbox ? How can this be done ? Thanks in advance
I set the value of 2 labels using this code . Now need to set a listbox as well.
$(document).ready(function() {
$.ajax({
type: "POST",
url: "Web开发者_Go百科Form2.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);
}
});
});
This is the object of this class returned by the webmethod
public class StatusViewModel
{
public string Label1 { get; set; }
public string Label2 { get; set; }
public List<string> ListBox { get; set; }
}
If I understood correctly, ListBox are already at the page, let it have server 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 %>')}
Try to return a list and then bind your list to your listbox.
$.each(ListBox, function(index, item) {
$("#ListBoxtoBeFilled").get(0).options[$("#ListBoxtoBeFilled").get(0).options.length] = new Option(item);
});
精彩评论