我需要在用户名或密码为空时禁用登录按钮。
请查找下面的代码:
$(document).ready(function(){
$('input').on('keyup blur mouseenter', function(e) {
if($("#userName").val().length!==0 && $.trim($('#userName').val()) !== '' && $("#password").val().length!==0 && $.trim($('#password').val())!== ''){
$('#submitLogin').attr('disabled',false);
}
});
});
请在jsp页面中找到下面的提交输入:
<input class="button" name="submit" type="submit" id ="submitLogin"value="Login" disabled="true" ></div>
我设法在 jquery 中做到这一点,如上所示,但我的问题是浏览器保存的用户名和密码。
这是当用户首次登录应用程序浏览器要求用户保存用户名和密码并且用户单击"is"时。
因此,一旦保存了用户名/密码,下次用户打开登录页面时,用户名和密码就已填写,但登录按钮仍处于禁用状态。
它被禁用,因为即使输入的用户名/密码已经填写,当它调用 jquery 方法时,两个输入文本的值为 null。
但是,当用户再次单击时,用户名和密码将被初始化并启用提交登录。
知道如何在页面首次加载时获取保存的用户名和密码的值,以便启用登录输入吗?
预先感谢您的帮助。
最佳答案
尝试 $.removeAttr()
、$.change()
并使用输入 input class="button"name="submit"type="submit “id="submitLogin"value="Login">
像这样
$(document).ready(function() {
if ($("#userName").val().length !== 0 && $.trim($('#userName').val()) !== '' && $("#password").val().length !== 0 && $.trim($('#password').val()) !== '') {
$('#submitLogin').removeAttr('disabled');
} else {
$('#submitLogin').attr('disabled', "");
}
$('input').on('change keyup blur mouseenter ', function(e) {
if ($("#userName").val().length !== 0 && $.trim($('#userName').val()) !== '' && $("#password").val().length !== 0 && $.trim($('#password').val()) !== '') {
$('#submitLogin').removeAttr('disabled');
} else {
$('#submitLogin').attr('disabled', "");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type='text' id='userName' />
<input type="password" id="password"/>
<input class="button" name="submit" type="submit" id="submitLogin" value="Login">
关于javascript - 如何识别jquery中保存的浏览器用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36595363/