我只是在选中了一个或两个复选框时才尝试制作表单。我知道jQuery validate直接支持这个,但它不起作用,我无法弄清楚为什么。
这是标记:
<form id="text_opt">
<input id="promos" name="promos" type="checkbox" /><label for="promos">Special Promotions</label>
<input id="products" name="products" type="checkbox" /><label for="products">New Products & Services</label>
<div class="mobile-wrap">
<input id="text_phone" name="text_phone" type="text" placeholder="Mobile Phone Number" required/>
</div>
<p class="text-msg">*Standard Messaging fees and rates apply</p>
这是功能:
function isTextChecked() {
return jQuery('input#promos').is(':checked') || jQuery('input#products').is(':checked')
}
这是jQuery验证调用:
jQuery('form#text_opt').validate({
rules: {
text_number: {
required: isTextChecked()
}
}
});
如果我在控制台中调用isTextChecked()
函数,它会按照我的预期返回true / false,但由于某种原因,无论如何都需要该字段。
答案 0 :(得分:0)
您已为text_number字段设置了必需的规则,但在您的表单中,字段名称为text_phone。它还有一个必需的属性,也应该删除。
jQuery('form#text_opt').validate({
rules: {
text_phone: {
required: isTextChecked()
}
}
});