为复选框数组放置错误消息

2024-02-20

我正在使用 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>

------------------------------------------已编辑-------------------- ------

我发现我可以使用错误放置,但我不知道如何显示ONLY表页脚或第二个字段集中其他位置的复选框数组的错误消息。

希望你能帮助我。


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

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所有复选框上的类。

See a JSFiddle 上的工作演示 http://jsfiddle.net/8L5C2/具有多个复选框。

EDIT:

Here http://jsfiddle.net/Z8hWg/35/是您自己的代码,并实施了上述解决方案。

希望这可以帮助 !

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为复选框数组放置错误消息 的相关文章