【发布时间】:2019-01-14 09:05:42
【问题描述】:
当select2 中的错误替换出现在框上方时,我想要框下方的错误替换。这是我的 HTML:
<form id="form" class="form_required">
<select type="text" id="test1" class="form-control select2" required>
<option value=""> ();</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');
}
}
不使用select2 时,错误替换显示在框下方,类似数字2,但使用select2 时,错误替换显示在框上方..
【问题讨论】:
-
Integrating jQuery Validate 与 Select2 之前已被询问和回答。请使用搜索。
标签: html css twitter-bootstrap jquery-validate jquery-select2