jQuery 如何使用jquery查找服务器控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5882189/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:57:47  来源:igfitidea点击:

How to find server control using jquery?

jqueryasp.net

提问by Ulhas Tuscano

How to find server control using jquery e.g

如何使用 jquery 查找服务器控件,例如

$(".tab1").find("<%=lblTab1Heading.ClientID%>"); throws exception thrown & not caught

回答by BrunoLM

You missed the #for IDs

你错过了#ID

              // here
$(".tab1").find("#<%=lblTab1Heading.ClientID%>");

If for some reason it is not working with the template parser, you can use the $=selector, like:

如果由于某种原因它不能与模板解析器一起使用,您可以使用$=选择器,例如:

$(".tab1").find("[id$=lblTab1Heading]");

jQuery API

jQuery API

回答by Tom Gullen

Try:

尝试:

var MyControl = $("#<%=lblTab1Heading.ClientID%>");

Because it has an ID, you can simply select it on it's ID which is done with the hash:

因为它有一个 ID,你可以简单地在它的 ID 上选择它,这是用哈希完成的:

$('#ElementID')

$('#ElementID')

回答by Tim Rogers

You need a # sign in your selector. Try

您需要在选择器中使用 # 符号。尝试

$(".tab1").find("#<%=lblTab1Heading.ClientID%>");

回答by Ibad Baig

Though I'm too late for the answer but I guess this code will also help viewers finding it difficult to get the server control ID from JQuery

虽然我回答得太晚了,但我想这段代码也将帮助查看者发现很难从 JQuery 获取服务器控件 ID

function GetClientID(id, context) {
   var el = $("#" + id, context);
   if (el.length < 1)
   el = $("[id$=_" + id + "]", context);
   return el;
}

and how you should call it

以及你应该如何称呼它

var clientId = GetClientID("serverControlId").attr("id");
var serverControl = document.getElementById(clientId);