简体   繁体   中英

Get Asp control id using jQuery

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

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());
 });

您需要选择选定的选项,然后获取其值。使用:

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

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM