【发布时间】:2011-11-20 14:23:05
【问题描述】:
使用这个 jQuery 验证插件。
http://www.benjaminkeen.com/software/rsv/jquery/index.php
我想用JS验证一个复选框组,但是当我使用语句时:
rules.push("required,accomodationtype[],Please check all that apply");
或
rules.push("required,accomodationtype\[\],Please check all that apply");
对于这种复选框组:
<INPUT type="checkbox" name="accomodationtype[]" id="accomodationtype_0" value="hotel1">Hotel1<BR>
<INPUT type="checkbox" name="accomodationtype[]" id="accomodationtype_1" value="hotel2">Hotel2<BR>
<INPUT type="checkbox" name="accomodationtype[]" id="accomodationtype_2" value="hotel3">Hotel3<BR>
<INPUT type="checkbox" name="accomodationtype[]" id="accomodationtype_5" value="other"> Other (please specify)<BR>
<INPUT type="text" name="accomodationtypeother" id="accomodationtypeother">
它不会验证并立即发布表单。我不确定我是否做错了什么。
更新
我有自定义错误处理程序。尝试了 Alper 的建议,收到此错误消息:errorInfo[i][0].focus is not a function
function errorHandler3(f, errorInfo)
{
for (var i=0; i<errorInfo.length; i++)
{
// errorInfo[i][0] contains the form field node that just failed the validation, e.g.
errorInfo[i][0].focus();
// errorInfo[i][1] contains the error string to display for this failed field, e.g.
$.notifyBar({
cls: "error",
html: errorInfo[i][1]
});
}
if (errorInfo.length == 0) tcrform_submit();
return false;
}
【问题讨论】:
-
在 jQuery 调用中尝试使用 accomationtype 而不是 accomationtype[]?
-
报错,该字段不存在
-
具有讽刺意味的是,您链接的验证器的不那么全面的演示页面没有显示如何验证复选框或复选框组。如果您打算使用该验证器,我会联系作者。
-
试过了,他没有回答。使用这个验证器,因为使用非常简单。您可以创建自定义错误处理程序。
-
检查这个小提琴:jsfiddle.net/Lz5gg/1 它按预期工作。
标签: javascript jquery ajax validation