简体   繁体   English

jQuery验证,将错误消息放在复选框和单选按钮组的下面

[英]jQuery validation, put error message below groups of checkboxes and radio buttons

I have a form that has a few groups of checkboxes and radio buttons. 我有一个包含几组复选框和单选按钮的表单。 The error message for the radio buttons always shows up directly after the first radio button in the group. 单选按钮的错误消息总是总是直接出现在组中的第一个单选按钮之后。 I was able to get the error messages to display below the groups of checkboxes. 我能够将错误消息显示在复选框组的下方。 However, I'm not having any luck getting the same to happen for radio buttons. 但是,对于单选按钮,我没有任何运气能做到同样的事情。 All of the name attributes for the radio and checkboxes are different (ugh). 单选框和复选框的所有名称属性都是不同的(ugh)。 Here's the code I currently have... 这是我目前拥有的代码...

jQuery("body.page-user-register input[type=checkbox]").addClass("certboxes");
jQuery.validator.addMethod('certboxes', function (value) {
          return jQuery('.certboxes:checked').size() > 0; }, 'Please check at least one     box.');
var checkboxes = jQuery('.certboxes');
var checkbox_names = jQuery.map(checkboxes, function(e,i) { return jQuery(e).attr("name")}).join(" ");


groups: {checks: checkbox_names},
    errorPlacement: function(error, element) {
         if (element.attr("type") == "checkbox") {
           error.insertAfter(checkboxes.parent().last());
           }
         else {
           error.insertAfter(element); 
           }
}

I was looking for a solution to this problem, and found this related (and solved) question. 我一直在寻找解决此问题的方法,并且发现了这个相关(已解决)的问题。 Updating for anyone who stumbles on this later. 为以后偶然发现此问题的任何人更新。

Jquery validation error placement (radio buttons) jQuery验证错误放置(单选按钮)

This worked for me: 这为我工作:

errorPlacement: function(error, element) {
     error.appendTo(element.parent());
}

Alternatively, one can use: 或者,可以使用:

errorPlacement: function(error, element) {
    error.appendTo(element.parent().siblings('.new container for errors'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM