jQuery Validation插件errorPlacement for checkbox

mOn*_*Ona 13 checkbox jquery jquery-validate errorplacement

我使用jQuery Validation插件来验证我的表单.我的问题是复选框的错误消息的位置.请看这个图像:

在此输入图像描述

这是与复选框+ jQuery验证相关的html部分:

<fieldset id = "q8"> <legend class="Q8"></legend>
<label> What are your favourite genres of movies?<span>*</span></label>
<div class="fieldset content"> 

<style type="text/css">
 .checkbox-grid li {
 display: block;
 float: left;
 width: 25%;
}     
</style>

<ul class="checkbox-grid">
<li><input type="checkbox" name="q8[]" value="Action"><label for="checkgenre[]">Action</label></li>
<li><input type="checkbox" name="q8[]" value="Adventure"><label for="checkgenre[]">Adventure</label></li>
<li><input type="checkbox" name="q8[]" value="Comedy"><label for="checkgenre[]">Comedy</label></li>
<li><input type="checkbox" name="q8[]" value="Animation"><label for="checkgenre[]">Animation</label></li>
<li><input type="checkbox" name="q8[]" value="Drama"><label for="checkgenre[]">Drama</label></li> 
<li><input type="checkbox" name="q8[]" value="Romance"><label for="checkgenre[]">Romance</label></li>
  //Continued the same for OTHER CHECKBOXES 

</div>
</fieldset>

  //html for other questions...

 <input class="mainForm" type="submit" name="continue" value="Save and Continue" />  

</form>

<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>

<script>
$(document).ready(function() {

 $('#form2').validate({ // initialize the plugin
    errorLabelContainer: "#messageBox",
    wrapper: "li",

    rules: {
       "q8[]": {
            required: true,
        },
       "q9[]": {
            required: true,
        },
        q10: {
            required: true,
        },
        q11: {
            required: true,
        },
        q12: {
            required: true,
        }

    },

      errorPlacement: function(error, element) {

      if (element.attr("type") == "radio") {
         error.insertBefore(element);
       } else {
         error.insertBefore(element);

    }
   }

 }); 
});
</script>
Run Code Online (Sandbox Code Playgroud)

如果可以解决此问题或在问题旁边显示错误消息(对于所有输入类型),有人可以帮助我吗?(我的意思是与问题完全相同的行,在*之后)

谢谢

Spa*_*rky 21

我添加了一个classlabel,以便您的问题找到更简单...

<label class="question">What are your favourite genres of movies?<span>*</span></label>
Run Code Online (Sandbox Code Playgroud)

然后你只需要练习"DOM遍历"技术.研究这里的方法.

  • $(element).parents('div')找到div你的容器element.

  • 然后.prev($('.question'))让你到达.question那个以前的第一个兄弟姐妹div.

把它们放在一起,它会在问题后面插入错误信息.

error.insertAfter($(element).parents('div').prev($('.question')))
Run Code Online (Sandbox Code Playgroud)

errorPlacement:

errorPlacement: function (error, element) {
    if (element.attr("type") == "checkbox") {
        error.insertAfter($(element).parents('div').prev($('.question')));
    } else {
        // something else if it's not a checkbox
    }
}
Run Code Online (Sandbox Code Playgroud)

演示:http: //jsfiddle.net/sgsvtd6y/

也许您不需要条件,因为该技术应该适用于您的所有input类型.

errorPlacement: function (error, element) {
    error.insertAfter($(element).parents('div').prev($('.question'))); 
}
Run Code Online (Sandbox Code Playgroud)

否则,如果您的HTML不同,那么您可以使用带有略微修改的DOM遍历的条件.