I'm trying to use the following code to add an option to a dropdown list in ASP.NET. Any ideas why this doesn't work? I tried Googling but can't figure out why this won't work.
我正在尝试使用以下代码向ASP.NET中的下拉列表添加一个选项。你知道为什么这行不通吗?我试着用谷歌搜索,但不知道为什么这行不通。
What shoud the code do? I have an ASP.NET dropdown list. I want to access the dropdown list by name and add an item to the list. The item should have descriptive text of "Some Text" and a value of "123".
代码应该做什么?我有一个ASP。净下拉列表中。我想按名称访问下拉列表并向列表中添加一个项目。项目应具有“某些文本”的描述性文本和“123”的值。
Thanks!
谢谢!
$("#ddlCategory").append($("<option>Some Text</option>").val(1).html("123"));
5 个解决方案
#1
11
var newOption = "<option value='"+"1"+"'>Some Text</option>";
$("#ddlCategory").append(newOption);
#2
9
You can try
你可以试着
$("#ddlCategory").append($("<option value='123'>Some Text</option>");
Or
或
$('#ddlCategory').
append($("<option></option>").
attr("value", "123").
text("Some Text"));
2nd code snippet from this question What is the best way to add options to a select from an array with jQuery?
从这个问题的第2个代码片段中,从jQuery数组中添加选项的最佳方式是什么?
#3
0
Have you tested that 1) your jquery is correct and works in a flat HTML file and 2) that you are using the correct Id - ASP.NET changes Ids dynamically on elements that runat="server", so you might want to try:
您是否测试过1)您的jquery是正确的,并且在一个平面HTML文件中工作,2)您正在使用正确的Id - ASP。NET对runat=“server”的元素动态更改id,因此您可能想尝试:
$('#<%=ddlCategory.ClientID%>').append(...etc etc
That will get you the correct Id from the ASP.NET page class.
这将从ASP中获得正确的Id。净页面类。
#4
0
What if you change it to
如果你把它改成!
$("#ddlCategory").append($("<option></option>").attr("value", "1").text("Some Text"));
#5
0
Trying to add options to an ASP.Net dropdown list with client-side code is a bad idea. It introduces all sorts of postback problems. See this link for more details. You should either populate the dropdown completely client side, or trigger a partial postback to fill the list.
尝试向ASP添加选项。带有客户端代码的下拉列表是一个坏主意。它引入了各种回发问题。有关更多细节,请参见此链接。您应该完全填充下拉菜单客户端,或者触发部分回发来填充列表。
#1
11
var newOption = "<option value='"+"1"+"'>Some Text</option>";
$("#ddlCategory").append(newOption);
#2
9
You can try
你可以试着
$("#ddlCategory").append($("<option value='123'>Some Text</option>");
Or
或
$('#ddlCategory').
append($("<option></option>").
attr("value", "123").
text("Some Text"));
2nd code snippet from this question What is the best way to add options to a select from an array with jQuery?
从这个问题的第2个代码片段中,从jQuery数组中添加选项的最佳方式是什么?
#3
0
Have you tested that 1) your jquery is correct and works in a flat HTML file and 2) that you are using the correct Id - ASP.NET changes Ids dynamically on elements that runat="server", so you might want to try:
您是否测试过1)您的jquery是正确的,并且在一个平面HTML文件中工作,2)您正在使用正确的Id - ASP。NET对runat=“server”的元素动态更改id,因此您可能想尝试:
$('#<%=ddlCategory.ClientID%>').append(...etc etc
That will get you the correct Id from the ASP.NET page class.
这将从ASP中获得正确的Id。净页面类。
#4
0
What if you change it to
如果你把它改成!
$("#ddlCategory").append($("<option></option>").attr("value", "1").text("Some Text"));
#5
0
Trying to add options to an ASP.Net dropdown list with client-side code is a bad idea. It introduces all sorts of postback problems. See this link for more details. You should either populate the dropdown completely client side, or trigger a partial postback to fill the list.
尝试向ASP添加选项。带有客户端代码的下拉列表是一个坏主意。它引入了各种回发问题。有关更多细节,请参见此链接。您应该完全填充下拉菜单客户端,或者触发部分回发来填充列表。