getting selected value from .NET dropdown using JQuery
I am trying to get the value of a .NET dropdown list in JQuery, but I am only able to get the ID of the selected item, not the selected value. Is it possible to do this? This is my curre开发者_运维知识库nt code, which gets the ID of the selected item :
alert($('#<%=ddlReportPeriods.ClientID %>').val());
try
alert($('#<%=ddlReportPeriods.ClientID %> option:selected').text());
You can set an attribute of ASP.NET control which will not change the ID at run time, that is
ClientIDMode = "Static"
Then try,
alert($("#ddlReportPeriods option:selected").val());
if it's the same as a regular html dropdown box you can try
$('#<%=ddlReportPeriods.ClientID %> option:selected').val()
Try this:
alert($('#<%=ddlReportPeriods.ClientID %> OPTION:selected').val());
If what you want is the textual content of the currently-selected <option>
tag under your <select>
element, then you could do this:
var $sel = $('#<%=ddlReportPeriods.ClientID %>');
alert($($sel.attr('options')[$sel.attr('selectedIndex')).text());
edit — the other answers with suggestions to use the ":selected" qualifier are better than this ...
<asp:DropDownList ID="ddMainMenuList" runat="server" class="form-control">
</asp:DropDownList>
$("[id*=ddMainMenuList]").val("SomeValue");
This is the way you can assign some value to dropdown or textbox or any other asp control. Just you have to mention "id".
精彩评论