单击 asp 标签后如何将焦点设置在文本框和/或控件上?
我想在单击 asp 标签后将焦点设置在文本框和/或控件上?有人可以向我解释一下如何做到这一点吗?类似于做一个
<label for="txtBoxID">Blah</label>
I would like to set the focus on a textbox and/or control after clicking an asp label? Can some explain to me how to do this? Similar to doing a
<label for="txtBoxID">Blah</label>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您也可以执行此操作
或在点 4. 上
执行此操作并避免使用 JavaScript。
You can also do this
or on dot 4.
and avoid JavaScript.
这是编写和避免 JavaScript 的最佳方法
This is the Best way to write and avoid javascript
您可以使用 Javascript 或 jQuery 来完成此操作。
如果您需要在单击标签时在服务器端执行某些操作,则必须在后面的代码中处理相同的操作,然后设置客户端脚本在重新加载页面后启动。使用
RegisterStartupScript
进行同样的操作。You can do it using Javascript or jQuery.
If you have a specific need of doing something in the server side on the click of the label, you shall have to handle the same in code behind and then set the client side script to fire up after reloading the page. Use
RegisterStartupScript
for the same.我假设您想完全在客户端完成以避免回发?
您可以使用 jQuery 来设置焦点。添加对 jQuery 库的脚本引用后,您可以在页面中使用以下 JavaScript:
“*[id$='txtBoxID']”选择器允许您选择文本框的 ASP.NET 服务器端 ID,而无需任何其他操作代码。基本上,它的意思是“选择 ID 以 txtBoxId 结尾的任何 DOM 元素”。
您可以使用以下 CDN 脚本参考将 jQuery 添加到您的页面:
I'm assuming you want to do it completely on the client side to avoid a postback?
You can use jQuery to set focus. After adding a script reference to the jQuery library, you can use the following JavaScript in your page:
The "*[id$='txtBoxID']" selector allows you to select the ASP.NET server side ID of your textbox without any additional code. Basically, it's saying "select any DOM element whose ID ends with txtBoxId."
You can add jQuery to your page with the following CDN script reference:
使用 jQuery 的更通用的解决方案:
只要正确定义
for
属性,就应该处理所有标签。A more generalized solution using jQuery:
Should handle all labels, as long as you correctly define the
for
attribute.