如果您检查
select2
图书馆,你会看到
select2-container
(负责select2下拉列表的容器)插入到
select
元素。下面是代码的一部分:
Select2.prototype._placeContainer = function ($container) {
$container.insertAfter(this.$element);
var width = this._resolveWidth(this.$element, this.options.get('width'));
if (width != null) {
$container.css('width', width);
}
};
我们可以很容易地
错误消息
从jquery验证之后
选择2容器
. 以下是
errorPlacement
选项:
errorPlacement: function (error, element) {
if(element.hasClass('select2') && element.next('.select2-container').length) {
error.insertAfter(element.next('.select2-container'));
}
....
以及显示结果的片段:
$('.select2').select2({container: 'body'})
$(".form_required").validate({
highlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-success').addClass('has-error');
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents('.form-control').removeClass('has-error').addClass('has-success');
},
errorPlacement: function (error, element) {
if(element.hasClass('select2') && element.next('.select2-container').length) {
error.insertAfter(element.next('.select2-container'));
} else if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
}
else if (element.prop('type') === 'radio' && element.parent('.radio-inline').length) {
error.insertAfter(element.parent().parent());
}
else if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
error.appendTo(element.parent().parent());
}
else {
error.insertAfter(element);
}
}
});
form {
margin: 20px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.full.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<form id="form" class="form_required form-horizontal">
<div class="form-group">
<div class="col-sm-8">
<select type="text" id="test1" class="form-control select2" required>
<option value=""> </option>
<option value="1">test1</option>
</select>
</div>
</div>
<div class="form-group">
<button type="button" onclick="insert()">Submit</button>
</div>
</form>
<form id="form2" class="form_required form-horizontal">
<div class="form-group">
<div class="col-sm-8">
<select type="text" id="test2" class="form-control" required>
<option value=""> </option>
<option value="1">test2</option>
</select>
</div>
</div>
<div class="form-group">
<button type="button" onclick="insert2()">Submit</button>
</div>
</form>
<script>
function insert(){
if ($('#form').valid()){
alert('OK');
}
}
function insert2(){
if ($('#form2').valid()){
alert('OK');
}
}
</script>
希望这有帮助。