使用jQuery获取ASP的控制ID [英] Get Asp control id using jQuery
问题描述
我知道这个问题已经被问了几次,但我不能够解决这个问题。我的问题很简单,我们如何使用jQuery获得ASP下拉菜单标识。
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
推荐答案
只需使用 ID选择器(# ID)你没有,如果你只有一个项目要获得外卡需要属性选择器。由于客户端ID
为您提供了元素的完整,准确的ID。
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());
如果您使用的是框架4级以上可以使用的 Control.ClientIDMode 保持服务器ID为客户端ID
If you are using framework 4 or above you can use Control.ClientIDMode to keep the Server id as ClientID
alert($('#ddlPortfolioName').val());
如果您在网格或中继器或ListView下拉,那么你将不得不使用包含通配符用属性选择器。
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());
});
这篇关于使用jQuery获取ASP的控制ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!