我是jquery的新手,我在使用我想要创建的表单上的提交按钮时遇到问题。当按钮被禁用时,如果所有验证都返回true,我想再次启用它,它将保持禁用状态。有人可以帮我解决这个问题。这是我的jQuery代码的一部分:
//some code...
$('form>input').blur(function () {
if (firstName == false || lastName==false || email==false || password1==false || password2==false)
$('#submitButton').prop('disabled',true);
if (firstName == true && lastName == true && email == true && password1 == true && password2 == true) //{ //$('#submitButton').prop('disabled',false);
$('#submitButton').removeAttr('disabled');
});
});
这是我的HTML代码的一部分:
<div id="mainDiv">
<form>
<label class="label">first name:</label><input type="text" class="firstName" name="firstName" />
<label class="label">last name:</label><input type="text" class="lastName" name="lastName" />
<label class="label">email address:</label><input type="text" class="email" name="email" />
<label class="label">create password:</label><input type="password" class="password1" name="password" />
<label class="label">confirm password:</label><input type="password" class="password2"/>
<input type="submit" id="submitButton" value="register" />
</form>
</div>
答案 0 :(得分:0)
您可以使用removeAttr()
功能删除disabled
属性:
$('#enable').on('click', function() {
$('#submit').removeAttr('disabled');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button id="submit" type="button" disabled>Submit</button>
<button id="enable" type="button">Enable</button>
&#13;
答案 1 :(得分:0)
这里的上下文是form>input
。给提交按钮id
,然后使用您现在使用的相同命令禁用该按钮
答案 2 :(得分:0)
请检查以下代码:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>
<body>
<form>
firstName <br />
<input type="text" id="firstName" name="firstName" /><br />
lastName<br />
<input type="text" id="lastName" name="lastName" /><br />
Email<br />
<input type="text" id="email" name="email" /><br />
password1<br />
<input type="password" id="password1" name="password1" /><br />
<input type="submit" id="register" value="Register" disabled="disabled" />
</form>
<div id="test">
</div>
<script type="text/javascript">
(function() {
$('form > input').keyup(function() {
var empty = false;
$('form > input').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
if (empty) {
$('#register').attr('disabled', 'disabled');
} else {
$('#register').removeAttr('disabled');
}
});
})()
</script>
</body>
</html>