开发者

How to set selected value of DropDownList with jQuery

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 w开发者_运维知识库ork:

$('#<%= rbDepartment.ClientID %>').change(function() {
    if ($("input[@name=GroupName]:checked").val() == "IS") {
        $('#ddlContactType').val('AM');
    }
});


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.


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:DropDownList runat="server" ID="ddlVersion" />

Select the currently selected drop down list option using 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜