我試圖隱藏部分窗體的按鈕被禁用,並讓用戶點擊按鈕來顯示其餘的窗體,當前面的字段被填入。任何人都可以幫忙嗎?這裏是我的代碼爲例:填寫字段時如何啓用按鈕?
HTML
<form>
<div id="group1">
<label>Field 1:</label>
<input type="text" class="field1"/><br/>
<label>Field 2:</label>
<input type="text" class="field2"/><br/>
<label>Field 3:</label>
<input type="text" class="field3"/><br/>
</div>
<div align="center">
<button id="show_form" onClick = "this.style.display= 'none'" disabled="disabled">
Enter Billing Info</button>
</div>
<div id="group2">
<label>Field 4:</label>
<input type="text" class="field4"/><br/>
<label>Field 5:</label>
<input type="text" class="field5"/><br/>
<label>Field 6:</label>
<input type="text" class="field6"/><br/>
</div>
</form>
JQUERY
<script>
$(document).ready(function() {
$('#group1').find('input[type="text"]').keyup(function() {
var flag = true;
$('#group1').find('input[type="text"]').each(function() {
if ($(this).val().length === 0) {
flag = false;
return;
}
});
if (flag) {
$("#show_form").prop("disabled", false);
} else {
$("#show_form").prop("disabled", true);
$("#group2").hide();
$("#show_form").show();
}
});
$("#group2").hide();
$("#show_form").click(function(){
$("#group2").show();
return false;
});
});
</script>
由於檢查輸入值的代碼只在文檔準備就緒時執行一次,因此無法保持字段被修改的軌道。此外,它並沒有驗證字段被填充在點擊按鈕上 – academo 2014-10-07 15:58:27
@academo是不是像用戶想要啓用按鈕只有當所有上述字段填充,因爲他最初設置按鈕爲「禁用」? – Ashutosh 2014-10-07 16:01:00
謝謝。這很好。如果他們決定刪除之前的某個字段,是否有辦法隱藏表單的第二部分? – Military911 2014-10-07 16:04:39