简体   繁体   English

使用jQuery获取ASP控件ID

[英]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. 我的问题很简单,我们如何使用jQuery获取asp下拉列表ID。

ASP 均价

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

JS 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. 这种逻辑对我来说不起作用,它显示了未定义的值, dropdown是否有可用的值,并且从第一个中选择了该值。 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. 只需使用ID选择器(“ #id”) ,如果只需要获取一项,则不需要带通配符的属性选择器。 As ClientID gives you complete and exact id of element. 由于ClientID为您提供了元素的完整且准确的ID。

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

If you are using framework 4 or above you can use Control.ClientIDMode to keep the Server id as ClientID 如果您使用的是框架4或更高版本,则可以使用Control.ClientIDMode将服务器ID保留为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. 如果您在网格,中继器或listView中有下拉菜单,则必须使用包含通配符的属性选择器。

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

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

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

You can try like dis 你可以像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 或者如果它在子页面中..den u hv传递服务器端ID ..例如,我的服务器端ID是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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