这个函数有一个语法错误,但我不清楚它是什么。 用户名存储在 cookie 中并由服务器检索(如果存在)。 页面加载时,如果用户名未填充,我希望焦点位于用户名文本框上。 否则我希望它出现在密码文本框中;
<h2>Log On</h2>
<p>
Please enter your username and password.
</p>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
<%: Html.LabelFor(m => m.UserName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.UserName) %>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(m => m.Password) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(m => m.Password) %>
<%: Html.ValidationMessageFor(m => m.Password) %>
</div>
<div class="editor-label">
<%: Html.CheckBoxFor(m => m.RememberMe) %>
<%: Html.LabelFor(m => m.RememberMe) %>
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
if (!$.trim($("#UserName")).length) { // zero-length string AFTER a trim
$("#UserName").focus();
}
else {
$("#Password").focus();
}
});
</script>
最佳答案
删除!
,然后尝试这个..
<script type="text/javascript" language="javascript">
$(document).ready(function () {
if ($.trim($("#UserName")).length > 0) { // zero-length string AFTER a trim
$("#Password").focus();
}
else {
$("#UserName").focus();
}
});
</script>
关于JQuery - 如果填充了用户名,我想将焦点设置在密码上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704890/