質問

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

他のヒント

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

$('#<%=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());
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top