如何将焦点设置在其他 ContentPlaceHolder 中的文本框上? - MVC2
您好,
我一直在互联网上搜索如何执行此操作,但到目前为止还没有运气。有很多解决方案发布,特别是在这个网站上(谢谢您),但似乎没有一个对我有用。这很可能是因为我现在才学习asp.net 4个月,缺乏必要的知识。
我想做的是在加载页面时将焦点设置在用户名输入文本框中。我见过很多解决方案,例如“defaultfocus”,body onload等等。任何人都可以给我一个简单的方法来完成这个任务吗?
我们的项目是一个 MVC 2 Web 应用程序。我们有一个主站点,它收集所有内容占位符。这是 logon.aspx 的链接
<div id="Clogin" onclick="logout();">
<asp:ContentPlaceHolder ID="LoginContent" runat="server" />
</div>
LogOn.aspx 实际上与自动生成的相同。这是我想要设置焦点的文本框:
<div class="login-field">
<%: Html.TextBoxFor(m => m.UserName)%>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
如何触发 onload 事件,无论是在母版页还是登录页面中,以便在该特定文本框上生成焦点?
提前致谢,并对不得不依赖您的知识表示歉意。
此致,
尼古拉斯
Greetings,
I've been scanning the internet on how to do this, but so far no luck. There are numerous solutions posted, especially on this website (thank you for that), but none seem to work for me. This will most probably be because I am only learning asp.net since 4 months now, and lack the necessary knowledge.
What I would like to do is set focus on the username input textbox when the page is loaded. I've seen many solutions, such as "defaultfocus", body onload, etc, etc. Anyone can give me a simple way to accomplish this?
Our project is a MVC 2 Web application. We have a master site, which collects all the contentplaceholders. Here is the link to the logon.aspx
<div id="Clogin" onclick="logout();">
<asp:ContentPlaceHolder ID="LoginContent" runat="server" />
</div>
The LogOn.aspx is practically the same as the auto-generated one. And here is the textbox I would like to set focus on:
<div class="login-field">
<%: Html.TextBoxFor(m => m.UserName)%>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
How can I trigger the onload event, be it in the master page, or the LogOn page, so that the focus is generated on that particular textbox?
Thanks in advance, and apologies for having to rely on your knowledge.
Sincerely,
Nicolas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您需要使用 JavaScript。如果您使用 jquery,您可以包含以下脚本:
另一种可能性是在 body 标记中执行此操作:
You will need to use javascript for this. If you are using jquery you could include the following script:
Another possibility is to do this in the body tag: