gpt4 book ai didi

javascript - Select2 & JqueryValidator 样式问题

转载 作者:太空狗 更新时间:2023-10-29 16:45:56 26 4
gpt4 key购买 nike

我正在使用 this libraryjquery.validate 库,我遇到了样式问题:enter image description here通常错误应该在选择列表下。
我的 JS 代码:

errorElement: 'p',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.form-group').length) {

error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
},
highlight: function(element) {
$(element).closest(".form-group").addClass("has-error").removeClass("has-success");
$(element).parent().find('.form-control-feedback').removeClass('glyphicon-ok').addClass('glyphicon-remove');
},
unhighlight: function(element) {
$(element).closest(".form-group").removeClass("has-error").addClass("has-success");
$(element).parent().find('.form-control-feedback').removeClass('glyphicon-remove').addClass('glyphicon-ok');
},

HTML 代码:

<div class="form-group has-feedback">
<label for="brands" class="col-sm-2">Brand :</label>
<div class="col-sm-9">
{!! Form::select('brands', $brandsArray, 'default' , ['class' => 'form-control combobox', 'id'=>'brands']) !!}
</div>
<div class="col-sm-1">
<!-- <a href="#" title="Add new brand" data-toggle="modal" data-target="#brandModal"><i class="fa fa-plus"></i></a> -->
<button type="button" class="btn btn-xs" title="Add new brand" id="addBrandLogo"><i class="fa fa-plus"></i></button>
</div>
</div>

有错误的 HTML 代码:

    <div class="col-sm-9">
<select name="brands" id="brands" class="form-control combobox select2-hidden-accessible" tabindex="-1" aria-hidden="true" aria-required="true" aria-describedby="brands-error" aria-invalid="true"><option selected="selected" value="">...</select>
<p id="brands-error" class="help-block">Please enter brand name</p>
<span class="select2 select2-container select2-container--default" dir="ltr" style="width: 497px;"><span class="selection">...</span></span>
</div>

最佳答案

这是我解决问题的代码:

    errorPlacement: function(error, element) {
if(element.parent('.form-group').length) {
error.insertAfter(element.parent());
} else {
//check if the element name is the select2 input
if (element.attr('name') == "brands")
//instead of insetring after the element directly
// insert the error after the next tag of element, after the select2 span.
error.insertAfter(element.next());
else
error.insertAfter(element);
}
},

关于javascript - Select2 & JqueryValidator 样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30014929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com