简体   繁体   English

jQuery验证选择2

[英]jQuery validation select2

I validate my form with jQuery validation plugin.我使用 jQuery 验证插件验证我的表单。 I change style dropdown menu with jQuery select2 plugin.我使用 jQuery select2 插件更改样式下拉菜单。 if validate callback error my input show/add class error box(red border) else show/add class success box(green border).如果验证回调错误我的输入显示/添加类错误框(红色边框)否则显示/添加类成功框(绿色边框)。 This worked in normal input but wont work in select2.这在正常输入中有效,但在 select2 中无效。 when I choose any value for select box error box(red border) not hide/switch to success box.当我为选择框错误框(红色边框)选择任何值时,不会隐藏/切换到成功框。 select2 worked after click submit button. select2 在单击提交按钮后起作用。

JS: JS:

$('form').validate({
    rules: {
        firstname: {
            minlength: 3,
            maxlength: 15,
            required: true
        },
        lastname: {
            minlength: 3,
            maxlength: 15,
            required: true
        }
    },
    highlight: function (element, errorClass, validClass) {
        if (element.type === "radio") {
            this.findByName(element.name).addClass(errorClass).removeClass(validClass);
        } else {
            $(element).closest('.form-group').removeClass('has-success has-feedback').addClass('has-error has-feedback');
            $(element).closest('.form-group').find('i.fa').remove();
            $(element).closest('.form-group').append('<i class="fa icon-plane icon-2x form-control-feedback"></i>')
        }
    },
    unhighlight: function (element, errorClass, validClass) {
        if (element.type === "radio") {
            this.findByName(element.name).removeClass(errorClass).addClass(validClass);
        } else {
            $(element).closest('.form-group').removeClass('has-error has-feedback').addClass('has-success has-feedback');
            $(element).closest('.form-group').find('i.fa').remove();
            $(element).closest('.form-group').append('<i class="fa icon-plane icon-2x  form-control-feedback"></i>');
        }
    },
    errorElement: 'span',
    errorClass: 'help-block',
    errorPlacement: function (error, element) {
        if (element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        } else {
            error.insertAfter(element);
        }
    }
});
$("#lstColors").select2({
    placeholder: "Select a Color",
    width: "200px"
});

how do fix this problem ?!如何解决这个问题?!

DEMO : http://jsfiddle.net/hTPY7/1413/演示:http: //jsfiddle.net/hTPY7/1413/

jquery.validate is not noticing that select2 changes. jquery.validate 没有注意到 select2 发生了变化。 While I am not sure why this is the case, you can work around this by noting when the form has been submitted (perhaps by applying the class 'submitted' to any form which was submitted by the user):虽然我不确定为什么会出现这种情况,但您可以通过注意何时提交表单来解决此问题(也许通过将“提交”类应用于用户提交的任何表单):

$('form').submit(function() { 
    $(this).addClass('submitted');
});

Then calling $().valid() when select2 changes, causing jquery.validate to revalidate the form:然后在 select2 更改时调用 $().valid() ,导致 jquery.validate 重新验证表单:

$('#lstColors').change(function() {
    if ($('form').hasClass('submitted'))
        $('form').valid();
});

Here is a jsFiddle fork which has it working: http://jsfiddle.net/Tdafh/1/这是一个可以正常工作的 jsFiddle 叉子:http: //jsfiddle.net/Tdafh/1/

I have answered this on a different StackOverflow question.我已经在另一个 StackOverflow 问题上回答了这个问题。 You have to take some extra steps to dig through the layered markup of a dropdown modified by select2.您必须采取一些额外的步骤来挖掘由 select2 修改的下拉列表的分层标记。 All the details and a demo can be seen there.所有细节和演示都可以在那里看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM