jQuery Validation插件的说明如下:
有四种方法可以提供错误消息
1,通过输入元素的title属性进行验证;
2,通过数据属性;
3,通过错误标签;
4,通过插件设置(选项消息)。
问题是关于第3项:
默认情况下,标签是在无效元素之后创建的
但是,当面对无线电和Bootstrap的复选框时,在每组的第一次输入后创建错误标签,我想在每组最后一次输入后放置错误标签,怎么做?
演示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/tether/1.1.1/css/tether.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<form id="demo1">
<div class="form-group row">
<label class="col-sm-2">Radios</label>
<div class="col-sm-10">
<div class="radio">
<label>
<input type="radio" name="gridRadios" id="gridRadios1" value="option1">
//error label is put here by default
Option1
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="gridRadios" id="gridRadios2" value="option2">
Option2
</label>
</div>
//I want to put the error label here
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">Checkbox</label>
<div class="col-sm-10">
<label class="checkbox">
<input type="checkbox" name="checkbox" id="checkbox1" value="option1">
//error label is put here by default
Option1
</label>
<label class="checkbox">
<input type="checkbox" name="checkbox" id="checkbox2" value="option2">Option2
</label>
<label class="checkbox">
<input type="checkbox" name="checkbox" id="checkbox3" value="option3">Option3
</label>
//I want to put the error label here
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-secondary">Submit</button>
</div>
</div>
</form>
</div>
<script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/tether/1.1.1/js/tether.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-validate/1.14.0/jquery.validate.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-validate/1.14.0/additional-methods.min.js"></script>
<script>
$(function () {
$("#demo1").validate({
rules: {
gridRadios: {
required: true
},
checkbox: {
required: true
}
},
messages: {
gridRadios: {
required: "required"
},
checkbox: {
required: "required"
}
}
});
});
</script>
</body>
</html>