asp.net-mvc 将焦点设置在文本框上 - MVC3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7856629/
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
Set Focus on a Textbox - MVC3
提问by rk1962
How can I set focus on "Amount" textbox on page load for the following Razor code in MVC3?
如何在 MVC3 中为以下 Razor 代码在页面加载时将焦点设置在“数量”文本框上?
<tr>
<th>
<div class="editor-label">
@Html.LabelFor(model => model.Amount)
</div>
</th>
<td>
<div class="editor-field">
@Html.EditorFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
</div>
</td>
</tr>
回答by Darin Dimitrov
You could provide an additional class to the containing div:
您可以为包含的 div 提供一个额外的类:
<div class="editor-field focus">
@Html.EditorFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
</div>
and then you could use a jQuery class selector:
然后你可以使用 jQuery 类选择器:
$(function() {
$('.focus :input').focus();
});
This being said you seem to have multiple Amounttexboxes in a table and obviously only one can have the focus at a time. So assuming you want this to be the first, you could do this:
话虽如此,您似乎Amount在一张表中有多个文本框,显然一次只能有一个焦点。因此,假设您希望这是第一个,您可以这样做:
$('.focus :input:first').focus();
回答by MaTya
with a html 5 compliant browser you set the autofocus
使用符合 html 5 的浏览器设置自动对焦
<input type="text" autofocus="autofocus" />
if you need IE support you need to do it with javascript
如果您需要 IE 支持,则需要使用 javascript
<input type="text" id=-"mytextbox"/>
<script type="text/javascript">document.getElementById('mytextbox').focus();</script>
in razor:
在剃须刀中:
@Html.EditorFor(model => model.Amount,new{@autofocus="autofocus"})
回答by kaspur
Open the /Views/Shared/Site.Master file and enter the following after the jquery script include this script:
打开 /Views/Shared/Site.Master 文件并在 jquery 脚本包含此脚本后输入以下内容:
<script type="text/javascript">
$(function () {
$("input[type=text]").first().focus();
});
</script>
This will set the focus to first textbox on every form in the app without writing any additional code!
这会将焦点设置为应用程序中每个表单的第一个文本框,而无需编写任何额外代码!
回答by Jon
The trick is that these input elements always have an idattribute, which you can get as a string with Html.IdFor. So you can focus the one you want with Javascript:
诀窍是这些输入元素总是有一个id属性,你可以用Html.IdFor. 因此,您可以使用 Javascript 关注您想要的内容:
document.getElementById("@Html.IdFor(model => model.Amount)").focus();
Or, if you prefer jQuery:
或者,如果您更喜欢 jQuery:
$("#" + "@Html.IdFor(model => model.Amount)").focus();
回答by STLDev
I'm using MVC4/Razor, and don't happen to have a Site.Master file in my project, However, I do have an _Layout.cshtml file that is applied to every page (Views/Shared/_Layout.cshtml), so I added this to an existing script in that file, as follows. This works well for me.
我正在使用 MVC4/Razor,并且在我的项目中碰巧没有 Site.Master 文件,但是,我确实有一个应用于每个页面的 _Layout.cshtml 文件(Views/Shared/_Layout.cshtml),所以我将其添加到该文件中的现有脚本中,如下所示。这对我很有效。
<script type="text/javascript">
$(function () {
$.ajaxSetup({
// ajaxSetup code here...
}
});
// Set the focus to the first textbox on every form in the app
$("input[type=text]").first().focus();
});
</script>

