Jquery bootstrap select2 plugin issue with validate plugin

空扰寡人 提交于 2019-11-30 17:04:05

Ok. Here is what worked for me

/* select 2 plugin */
$('select#foo').select2();

/* Validation */
    $("#test_form").validate({
        ignore: 'input[type=hidden]',
        rules:
        {
            foo: { required: true },
            bar: { required: true }
        },
        errorPlacement: function (error, element) {
            $(element).parent().addClass('has-error');
        },
        success: function (label, element) {
            $(element).parent().removeClass('has-error');
        },
        submitHandler: function(form) {
            alert("validation ok");
        }
    });

I added a "ignore: 'input[type=hidden]'," option because the Select2 script adds a hidden option to the original input and the Jquery Validator seems to ignore it. Then i changed the error class from "err" on the element to "has-error" on the parent element (the div with form-group class)

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