如果选择了所有可见的必填字段,我需要检查点击按钮。
HTML:
<input type="text" class="required-entry">
<input type="text" class="required-entry">
<input type="text" class="required-entry">
<div style="display:none">
<input type="text" class="required-entry">
</div>
<button class="check-fields">Check Fields</button>
Jquery的:
$('.check-fields').on('click', function () {
var value = $('.required-entry').filter(function () {
return this.value === '';
});
if (value.length == 0) {alert('Everything has a value.');
} else if (value.length > 0) {
alert('Please fill out all required fields.');
}
});
答案 0 :(得分:4)
那是因为你在隐藏的div中还有一个具有相同类的文本字段。总是空着的。您应该仅从可见文本元素中过滤掉:
var value = $('.required-entry:visible').filter(function () {
return this.value === '';
});
<强> Working Demo 强>