我已添加此JQuery - 在SO上找到以验证我的表单,以便在单击提交按钮之前必须填写字段,但该按钮从不取消禁用。
表格
<form action="homepage.php" method="POST" id="form">
<div class="medium-50 columns">
<label>Phone Number</label>
<input type="text" placeholder="Enter Your Phone Number" name="phone" />
</div>
<input type="submit" id="register" value="Submit" name="submit" disabled="disabled">
</form>
JQuery的
(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');
}
});
})()
有什么问题?
答案 0 :(得分:2)
它不起作用,因为&#39;输入&#39;不是&#39; form&#39;的孩子。
试试这个:
(function() {
$('form input').keyup(function() {
var empty = false;
$('form input').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
console.log(empty);
if (empty) {
$('#register').attr('disabled', 'disabled');
} else {
$('#register').removeAttr('disabled');
}
});
})()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="homepage.php" method="POST" id="form">
<div class="medium-50 columns">
<label>Phone Number</label>
<input type="text" placeholder="Enter Your Phone Number" name="phone" />
</div>
<input type="submit" id="register" value="Submit" name="submit" disabled="disabled">
&#13;