Bootstrap 3 + Chosen + jquery validate loses format

雨燕双飞 提交于 2019-12-10 21:13:33

问题


I have the code below http://jsfiddle.net/emamut/CBjmj/4/

$.validator.setDefaults({ ignore: ":hidden:not(select)" });
$('form').validate({
    highlight: function(element) {
        $(element).closest('.form-group').addClass('has-error');
    },
    unhighlight: function(element) {
        $(element).closest('.form-group').removeClass('has-error');
    },
    errorElement: 'span',
    errorClass: 'help-block',
    errorPlacement: function(error, element) {
        if(element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        } else {
            error.insertAfter(element);
        }
    }
});

but when select validate the formatting is lost and returns the normal format of Chosen


回答1:


In the errorPlacement option, when you use insertAfter it inserts the error message between the select element and the div.chosen-container. This fouls up the CSS used for the chosen widget. For example, if you use insertBefore, the problem goes away:

$.validator.setDefaults({ ignore: ":hidden:not(select)" });
    $('form').validate({
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error');
    },
    unhighlight: function(element) {
        $(element).closest('.form-group').removeClass('has-error');
    },
    errorElement: 'span',
    errorClass: 'help-block',
    errorPlacement: function(error, element) {
        if(element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        } else if (element.is('select')) {
            error.insertBefore(element);
        } else
        {
            error.insertAfter(element);
        }
    }
});



回答2:


If what you want is to add the error message bellow the chosen, you always can add it after adding this clause to the errorPlacement:

else if (element.is('select')) {
   error.insertAfter(element.siblings(".chosen-container"));
}

I've forked your fiddle: http://jsfiddle.net/alfupe/0Lwgmmav/



来源:https://stackoverflow.com/questions/24832213/bootstrap-3-chosen-jquery-validate-loses-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!