jQuery 验证需要回调
jQuery Validation required callback
我正在尝试制作一个只有在我的一个或两个复选框被选中时才需要的表单。我知道 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()
函数,它 returns true/false 就像我期望的那样,但出于某种原因,无论如何都需要该字段。
您已经为 text_number 字段设置了必需的规则,但在您的表单中字段名称是 text_phone。它还有一个必需的属性,也应将其删除。
jQuery('form#text_opt').validate({
rules: {
text_phone: {
required: isTextChecked()
}
}
});
我正在尝试制作一个只有在我的一个或两个复选框被选中时才需要的表单。我知道 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()
函数,它 returns true/false 就像我期望的那样,但出于某种原因,无论如何都需要该字段。
您已经为 text_number 字段设置了必需的规则,但在您的表单中字段名称是 text_phone。它还有一个必需的属性,也应将其删除。
jQuery('form#text_opt').validate({
rules: {
text_phone: {
required: isTextChecked()
}
}
});