我在源自此bootsnip的登录页面上使用CSS,HTML + C#和Jquery 3.1.1的组合。我正在尝试根据是否填充用户名和密码字段(长度> 0)来启用/禁用登录按钮。
有问题的剃刀代码:
<div class="form-group">
@Html.TextBoxFor(m => m.UserName, new { @class = "textbox", placeholder = "Username", id = "userName", name="userName", autofocus = "autofocus" })
@Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.PasswordFor(m => m.Password, new { @class = "textbox", placeholder = "Password", id="password", name="password"})
@Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
</div>
<div class="col-xs-6 form-group checkbox">
@Html.CheckBoxFor(m => m.RememberMe, new { @class = "form-control checkbox" })
<label for="checkbox1">Remember Me</label>
</div>
<div class="col-xs-6 form-group" style="padding:0px;">
<input type="submit" name="login-submit" id="login-submit" tabindex="4" class="btn btn-cust" value="Log In" onclick="form.submit()">
</div>
JQuery:
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
// Creates a disable state for input types
jQuery.fn.extend({
disable: function (state) {
return this.each(function () {
var $this = $(this);
if ($this.is('input'))
this.disabled = state;
else
$this.toggleClass('disabled', state);
});
}
});
//After each keypress checks if fields are populated
$(".textbox").keyup(function () {
var i = 0;
$(".textbox").each(function () {
if (this.value.length > 0) {
i++;
} else {
i--;
}
});
if (i == 2) {
$('input[name="login-submit"]').disable(false);
} else {
$('input[name="login-submit"]').disable(true);
}
});
// Checks if fields are populated from autofill
setInterval((function checkAutoFill() {
var i = 0;
$(".textbox").each(function () {
if ($(`#userName`).val().length > 0) {
i++;
} else {
i--;
}
});
if (i == 2) {
$('input[name="login-submit"]').disable(false);
} else {
$('input[name="login-submit"]').disable(true);
}
}), 5000);
</script>
当我没有存储用户名和密码时,keyup函数可以正常工作,而checkAutoFill也可以正常工作。但是,当我输入文本框时,其长度为null吗?装满之后。在我选择框内之前,情况一直如此。
我尝试过的事情: