如何在 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>
最佳答案
您可以为包含的 div 提供一个附加类:
<div class="editor-field focus">
@Html.EditorFor(model => model.Amount)
@Html.ValidationMessageFor(model => model.Amount)
</div>
然后你可以使用 jQuery 类选择器:
$(function() {
$('.focus :input').focus();
});
话虽如此,表格中似乎有多个 Amount
文本框,显然一次只有一个文本框可以获得焦点。因此,假设您希望这是第一个,您可以这样做:
$('.focus :input:first').focus();
关于asp.net-mvc - 将焦点设置在文本框上 - MVC3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856629/