jQuery 如何在aspx页面中的用户控件文本框上设置焦点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18310197/
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 set focus on user control textbox in aspx page?
提问by Sid M
How can I set focus on asp:TextBox which is placed inside a user control by using jquery/JavaScript function in aspx page?
如何通过在 aspx 页面中使用 jquery/JavaScript 函数将焦点设置在放置在用户控件内的 asp:TextBox 上?
采纳答案by Adil
You can use id selectorand use focus()method. For using jQuery you need to include jQueryand ensure your element is available in DOM
before you access it. You can use document.readyfor that.
您可以使用id 选择器并使用focus()方法。要使用 jQuery,您需要包含 jQuery并确保您的元素在访问DOM
之前可用。您可以为此使用document.ready。
$('#txt1').focus();
Or using plain JavaScript, focus()
或者使用普通的 JavaScript,focus()
document.getElementById('txt1').focus();
If you do not have ClientIDMode = static then you will need to use ClientID
如果您没有 ClientIDMode = static 那么您将需要使用 ClientID
$('#<%= txt1.Client %>').focus();
回答by Pouki
You can try with jQuery :
您可以尝试使用 jQuery :
$('#txt').focus();
回答by debabrata
function myfocus2() {
$('#<%=TextBox2.ClientID %>').focus();
}
There should be no space between =
and TextBox2
.
=
和之间不应有空格TextBox2
。