Get Asp control id using jQuery

此生再无相见时 提交于 2019-12-08 06:25:54

问题


I know that this question has been asked several times but I'm not able to solve this problem. My problem is simple how we get asp dropdown id using jQuery.

ASP

<asp:DropDownList ID="ddlNewPortfolioName" Width="150" runat="server" AutoPostBack="True"></asp:DropDownList>

JS

alert($("[id$='<%=ddlPortfolioName.ClientID %>']").val());

This logic not work for me it show undefined value, whether there are some value available in dropdown and out of these first one is selected. Please help me on this


回答1:


Simply use ID Selector (“#id”) you do not need attribute selector with wild card if you have only one item to get. As ClientID gives you complete and exact id of element.

alert($('#<%= ddlPortfolioName.ClientID %>').val());

If you are using framework 4 or above you can use Control.ClientIDMode to keep the Server id as ClientID

alert($('#ddlPortfolioName').val());

If you have dropdown in grid or repeater or listView then you will have to use contains wild card with attribute selector.

 $('[id*=ddlPortfolioName]').each(function(){
       alert($(this).val());
 });



回答2:


You need to select selected option and then get its value.use:

$('#<%=ddlPortfolioName.ClientID %> option:selected').val()



回答3:


You can try like dis

Var data=$("input[id*=ddlPortfolioName]").val();

alert(data);

or

alert($('#<%= ddlPortfolioName.ClientID %>').val());

or if its in child page..den u hv to pass the server side ID..for example my server side id is

alert($("#ctl00_MainContent_ddlPortfolioName").val());


来源:https://stackoverflow.com/questions/21905311/get-asp-control-id-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!