放置复选框数组的错误消息 [英] Placing error message for a checkbox array

查看:70
本文介绍了放置复选框数组的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery验证插件,它的工作原理很奇妙.除非我有一组复选框...否则错误消息将在第一个复选框之后显示...就像这样:

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>

------------------------------编辑------------- ------------

我发现我可以使用 errorPlacement ,但是我不知道如何在表页脚或内部的其他地方显示 ONLY 复选框数组的错误消息.第二个字段集.

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.

希望你能帮助我.

推荐答案

为什么不使用自定义验证方法?像这样:

Why not use a custom validation method ? Something like this:

jQuery:

// The custom validation method, returns FALSE (invalid) if there are
// no checkboxes (with a .one_required class) checked
$.validator.addMethod("one_required", function() {
    return $("#myform").find(".one_required:checked").length > 0;
}, 'Please select at least one vehicle.');

$("#myform").validate({
    // Use the built-in errorPlacement function to place the error message
    // outside the table holding the checkboxes if they are the ones that
    // didn't validate, otherwise use the default placement.
    errorPlacement: function(error, element) {
        if ($(element).hasClass("one_required")) {
            error.insertAfter($(element).closest("table"));
        } else {
            error.insertAfter(element);
        }
    }
});

HTML:

<form id="myform">
    <!-- table, rows, etc -->
    <td align="center"><input type="checkbox" class="one_required" name="selectItems[]" value="NA245852" /></td>
    <td>NA245852</td>
    <!-- more rows, end table, etc -->
    <br/>
    <input type="submit" value="Go, baby !">
</form>

由于jQuery Validate插件还可以验证元素(如果方法名称以class表示),因此只需在所有复选框上输出.one_required类即可.

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.

请参见在JSFiddle上的工作演示,其中包含多个复选框.

See a working demo on JSFiddle with multiple checkboxes.

此处是您自己的代码,并且已实现上述解决方案.

Here's your own code with the above solution implemented.

希望这会有所帮助!

这篇关于放置复选框数组的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆