jQuery验证select2.js中的errorPlacement

时间:2018-08-07 09:59:43

标签: html css twitter-bootstrap jquery-validate jquery-select2

select2中的错误替换显示在框上方时,我希望错误替换在框下方。这是我的HTML:

<form id="form" class="form_required">
    <select type="text" id="test1" class="form-control select2" required>
        <option value="">&nbsp();</option>
        <option value="1">1</option>
    </select>
    <button type="button" onclick="insert()">Submit</button>
</form>

这是JavaScript:

$('.select2').select2()

$(".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.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);
        }
    }
});

function insert(){
    if ($('#form').valid()){
        alert('OK');
    }
}

1)enter image description here 2)enter image description here

当不使用select2时,错误替换将显示在框2下方,而在使用select2时,错误替换将显示在框上方。

1 个答案:

答案 0 :(得分:1)

如果检查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);
 }
};

这使我们可以轻松地将来自select2-container之后的jquery-validate中的错误消息。这是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="">&nbsp;</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="">&nbsp;</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>

希望这会有所帮助。