简体   繁体   English

使用jQuery Validate在select2中输入有效答案后,删除类('has-error')

[英]Remove class('has-error') after entering valid answer in select2 using jquery validate

I am using select2 for dropdown and jquery validate. 我正在使用select2进行下拉和jquery验证。 After entering valid input, error class should be removed, and it is working with other inputs like texts, but select is retaining this class. 输入有效输入后,应删除错误类别,并且该错误类别可与其他输入(例如文本)一起使用,但select保留了该类别。 I tried doing so: 我尝试这样做:

$.validator.setDefaults({
    errorClass: 'help-block',
    errorElement: 'span',
    focusInvalid: true,
    highlight: function(element, errorClass, validClass)
    {

      var fieldtype = ($(element).hasClass('dateField') ? 'dateField' : 'normal');
      switch(fieldtype)
      {
        case "dateField":
          $(element).closest('.form-group').addClass('has-error');
          // $(element).select();
          break;
        default:
          $(element).closest('.form-group').addClass('has-error');
          break;
      }

    },
    unhighlight: function(element, errorClass, validClass)
    {
      if($(element).hasClass('select2-offscreen'))
      {
        $(element).closest('.form-group').removeClass('has-error');
        $(element).next('span').css({display: "block"});
      }
      else
      {
        $(element).closest('.form-group').removeClass('has-error');
      }   
    },

    errorPlacement: function (error, element) {
            var fieldtype = ($(element).hasClass('dateField') ? 'dateField' : 'normal');

            switch (fieldtype) {
                case "dateField":
                    if (window && window.console) {
                        console.log("Date type field type");
                    }
                    error.appendTo(element.parent().parent());
                    break;
                default:
                    if (window && window.console) {
                        console.log("Regular field type");
                    }
                    error.appendTo(element.parent());
                    break;
            }
        }
  });
  $('#employee-form').validate(
  {
    rules: {
            firstname: {
                alpha: true,
                required: true
            },
           },
messages: {.....
});

Here's my html: 这是我的html:

<div class="form-group">
  <label class="col-sm-4 control-label" for="designation">Designation <span class="input-required">*</span></label>
  <div class="col-sm-7">
    <select name="designation_id" class="form-control" id="designations">
     <option></option>
     <option value="<?php echo $designation->id; ?>"><?php echo $designation->title; ?></option>
     </select>
  </div>
</div>

Here's select2 initialization: 这是select2初始化:

$('#designations').select2({
    sortResults: function (results) {
        return results.sort();
    },
    placeholder: "Select designation",
    allowClear: true
});

I manually tried to remove errorClass and add style="display: none" to error element, but it doesn't work. 我手动尝试删除errorClass并将style =“ display:none”添加到error元素,但是它不起作用。 Help me please. 请帮帮我。

I think the setTimeout should do the job here. 我认为setTimeout应该在这里完成工作。 Please change your validate plugin settings as given here: 请按照以下说明更改您的验证插件设置:

unhighlight: function(element) {
    if (jQuery(element).is('#designations')) {
        setTimeout(function(){
            $(element).closest('.form-group').removeClass('has-error');
        },50);
    }
}

and add following code additionally: 并另外添加以下代码:

jQuery("#designations").on('change', function (evt) {
    $thisVal = jQuery("#designations").val();
    if($thisVal != '') {
        setTimeout(function(){
            jQuery("#test-form").validate().element('#designations');
        },100);
    }           
});

If you still have issues, kindly send the url and i will test it for you. 如果您仍有问题,请发送网址,我会为您测试。

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

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