使用 jQuery 检索服务器控件的 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5666011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
retrieve ID of server control using jQuery
提问by AGuyCalledGerald
How do I get the ID of a server control with jQuery?
如何使用 jQuery 获取服务器控件的 ID?
E.g. I have
例如我有
<asp:Label ID="label1" runat="server""></asp:Label>
<asp:Label ID="label1" runat="server""></asp:Label>
and now I want to get "label1",
现在我想得到“label1”,
var id = ??
回答by pstarkov
If you use ASP.NET 4.0 you can set attribute ClientIDMode="Static" and your code will looks following way:
如果您使用 ASP.NET 4.0,您可以设置属性 ClientIDMode="Static" 并且您的代码将如下所示:
<asp:Label ID="label1" runat="server" ClientIDMode="Static"></asp:Label>
js:
js:
var id = 'label1';
回答by Hyman Marchetti
var labelID = $('#<%= label1.ClientID %>');
You need to get the client ID.
您需要获取客户端 ID。
If you just need the ID, and not the actual value of the control, then you don't even need jQuery.
如果您只需要 ID,而不需要控件的实际值,那么您甚至不需要 jQuery。
var labelID = '<%= label1.ClientID %>';
回答by Roman
var $lblObj = $("label[id$='label1']:first")
回答by selva
Are you using master page. If yes give ContentPlaceHolderID along with control id.
您是否使用母版页。如果是,请提供 ContentPlaceHolderID 和控件 ID。
Eg:
例如:
jQuery("#ContentPlaceHolderID_ControlId").val;
jQuery("#body_label1").text;
You can see this in Viewsource
你可以在 Viewsource 中看到这个
回答by Ira Rainey
jQuery runs on the client side so would only be able to access the ID of the html element rather than the asp control on the server.
jQuery 在客户端运行,因此只能访问 html 元素的 ID,而不是服务器上的 asp 控件。
回答by Steve Wellens
Labels render as span tags. So if you want to select all the Labels:
标签呈现为跨度标签。因此,如果要选择所有标签:
$(document).ready(function()
{
$Labels = $("span");
$Labels.each(function()
{
alert(this.id);
});
});