jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 为复选框数组放置错误消息大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用jQuery的Validation插件,它可以创造奇迹.除非我有一组复选框…错误消息将在第一个复选框后显示……如下所示:

<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 ——————- ——

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

希望你能帮助我.

解决方法

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

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插件也可以验证元素,只需在所有复选框上输出.one_required类.

查看具有多个复选框的working demo on JSFiddle.

编辑:

Here是您自己的代码,实现了上述解决方案.

希望这可以帮助 !

大佬总结

以上是大佬教程为你收集整理的jquery – 为复选框数组放置错误消息全部内容,希望文章能够帮你解决jquery – 为复选框数组放置错误消息所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: