如何使用 jQuery 设置 DropDownList 的选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7437381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set selected value of DropDownList with jQuery
提问by Bitmask
How do i change the value of the dropdownlist that has its data set via the datasource
如何通过数据源更改具有其数据集的下拉列表的值
ddlContacts.DataSource = Data;
ddlContacts.DataBind();
I have tried this but does not work:
我试过这个,但不起作用:
$('#<%= rbDepartment.ClientID %>').change(function() {
if ($("input[@name=GroupName]:checked").val() == "IS") {
$('#ddlContactType').val('AM');
}
});
回答by James Johnson
Give this a shot:
试一试:
var selectedValue = $("#<%=ddlContacts.ClientID%> option:selected").val();
Just noticed that you're trying to set the value:
刚刚注意到您正在尝试设置值:
$("#<%=ddlContacts.ClientID%>").val("thevalue");
Remember, when dealing with ASP.NET controls on the client side, you have to use the ClientID
.
请记住,在客户端处理 ASP.NET 控件时,您必须使用ClientID
.
回答by Renjith Thomas
I had the same problem of getting the current selected value from a drop down list and setting a new value as selected. Below is the code I used and it is working:
我遇到了从下拉列表中获取当前选定值并将新值设置为选定值的相同问题。下面是我使用的代码,它正在工作:
ASP .Net code:
ASP .Net 代码:
<asp:DropDownList runat="server" ID="ddlVersion" />
Select the currently selected drop down list option using JQuery:
使用 JQuery 选择当前选择的下拉列表选项:
var selectedVersion = $('#<%=ddlVersion.ClientID%> option:selected').text();
To set the selected value in drop down list:
要在下拉列表中设置选定的值:
$('#<%=ddlVersion.ClientID%> option:selected').text(currentVersion);
This code is working perfectly fine.
这段代码工作得很好。