我見過很多人禁用按鈕的例子,如果文本框是空的,但我還沒有找到任何將禁用按鈕只有某些文本框,日期和選擇字段。我是Jquery的新手,我知道它是僞代碼,但你可以明白。我必須致電哪個Jquery函數才能不斷檢查?如何在if子句中使用or語句來確定是否有任何文本框字段爲空?如何禁用提交按鈕時,文本框,選擇和日期是空的,並在其完成時啓用
HTML
<form id="myform">
Username<br />
<input type="text" id="user_input" name="username" /><br />
Password<br />
<input type="password" id="pass_input" name="password" /><br />
Confirm Password<br />
<input type="password" id="v_pass_input" name="v_password" /><br />
Email<br />
<input type="text" id="email" name="email" /><br />
Birthday<br />
<input type="date" id="bday" name="birthday" /><br />
Sex<br />
<select name="sex" id="sex">
<option>Male</option>
<option>female</option>
</select>
<input type="submit" id="register" value="Register" disabled="disabled" />
</form>
的Javascript
<script type="text/javascript>
(function() {
$('#myform > input').keyup(function() {
var empty = false;
$('form > input #bday').each(function() {
if ($(this).val() == '') {
empty = true;
}
});
if (empty) {
$('#register').attr('disabled', 'disabled');
} else {
$('#register').removeAttr('disabled');
}
});
})()
</script>
你想檢查哪一個空或不空? – putvande
你看過HTML5表單驗證嗎? http://www.html5rocks.com/en/tutorials/forms/constraintvalidation/ – cimmanon
檢查是否可用瀏覽器http://html5pattern.com/ – loveNoHate