为复选框数组放置错误消息
我正在使用 jQuery 的验证插件,它的效果非常好。除非我有一组复选框...错误消息将在第一个复选框之后显示...就像这样:
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<td align="center">
<input type="checkbox" name="selectItems" value="<c:out value="${item.numberPlate}"/>" />
</td>
<!--some other columns-->
</tr>
</c:forEach>
</tbody>
------------------------------已编辑-------------- ------------
我发现我可以使用 errorPlacement ,但我不知道如何仅显示错误消息表页脚或第二个字段集中其他位置的复选框数组的位置。
希望你能帮助我。
I am using the Validation Plugin for jQuery and it works wonders. Except when I have a group of checkboxes...the error messages will display right after the first checkbox...like so:
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<td align="center">
<input type="checkbox" name="selectItems" value="<c:out value="${item.numberPlate}"/>" />
</td>
<!--some other columns-->
</tr>
</c:forEach>
</tbody>
------------------------------EDITED-------------------------
I found that I can use errorPlacement , but I have no idea how to show ONLY the error message of the checkbox array in the table footer or somewhere else inside the second fieldset.
Hope you can help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用自定义验证方法?像这样的东西:
jQuery:
HTML:
由于 jQuery Validate 插件还可以验证元素(如果方法名称以
class
形式出现),只需简单地在所有复选框上输出.one_required
类。请参阅带有多个复选框的 JSFiddle 上的工作演示。
编辑:
这里是您自己的代码,并实现了上述解决方案。
希望这有帮助!
Why not use a custom validation method ? Something like this:
jQuery:
HTML:
Since the jQuery Validate plugin can also validate an element if the method name is present as a
class
, simply output the.one_required
class on all checkboxes.See a working demo on JSFiddle with multiple checkboxes.
EDIT:
Here's your own code with the above solution implemented.
Hope this helps !
您可以使用 errorLabelContainer 配置选项..
定义一个带有像 errorMessages 这样的 id 的 div,然后更改您的验证方法调用,如下所示:
现在所有错误消息都将显示在该 div 中。
编辑:更新了以下答案以反映问题的更新:
要更改错误消息显示的位置,我可以考虑使用 errorPlacement 配置参数。
你可以使用类似的东西:
定义一个 div 或 ap 并继续将复选框的消息附加到该元素。
jsFiddle URL: http://jsfiddle.net/Z8hWg/28/
编辑:
将您的 JavaScript 更改为如下:
You can use errorLabelContainer configuration option..
define a div with an id like errorMessages and then change your validate method call as follows:
Now all the error messages will be displayed in the div.
Edit: Updated the answer below to reflect the updates to the question:
To change the location of error messages displayed, I can think of using the errorPlacement config parameter.
You can use something like:
Define a div or a p and keep appending the messages for the checkboxes to this element.
jsFiddle URL: http://jsfiddle.net/Z8hWg/28/
EDIT:
Change your javascript to as follows: