javascript 如何使用java脚本查找asp控件ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10618196/
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
How to find a asp control id using java script?
提问by user1369925
I am using following code to find control id.
我正在使用以下代码来查找控件 ID。
alert(document.getElementById("<%# TextBox1.ClientId %>").value )
but this code is giving error "object required". Please help me.
但是这段代码给出了错误“需要对象”。请帮我。
采纳答案by Habib
Either use
要么使用
alert(document.getElementById("<%= TextBox1.ClientId %>").value )
or set ClientIDMode="Static"
for textbox and then
或设置 ClientIDMode="Static"
为文本框,然后
alert(document.getElementById("<%= TextBox1 %>").value )
回答by Josh Darnell
You need to use '=', not '#'
您需要使用“=”,而不是“#”
alert(document.getElementById("<%= TextBox1.ClientId %>").value );
The "<%#
" symbol is an inline expression used for databinding.
" <%#
" 符号是用于数据绑定的内联表达式。
The "<%=
" symbol there is used for display / translation purposes. It basically does a Response.Write
of just the value of the .ClientID
property of your server control.
“ <%=
”符号用于显示/翻译目的。它基本上Response.Write
只执行.ClientID
服务器控件的属性值。
See this article on inline expressions in asp.netfor more info.
有关详细信息,请参阅有关asp.net 中的内联表达式的这篇文章。
回答by Imran Rizvi
replace # with = in the given statement updated statement is
在给定的语句中用 = 替换 # 更新语句是
alert(document.getElementById("<%= TextBox1.ClientId %>").value);
回答by Saurabh
alert(document.getElementById('Id of control').value )
You can get the exact id of control by view source.
您可以通过查看源获得控件的确切 id。
回答by Andrei
In the context of JavaScript, which is on the client side "<%# TextBox1.ClientId %>"
has no meaning because this will be translated by ASP into to a different ID based on how you configure TextBox1.ClientIDMode
for which you have 4 modes described here Control.ClientIDMode
在 JavaScript 的上下文中,客户端"<%# TextBox1.ClientId %>"
没有任何意义,因为这将被 ASP 根据您的配置TextBox1.ClientIDMode
方式转换为不同的 ID ,您在这里描述了 4 种模式Control.ClientIDMode