使用 jQuery Validate with Select2 验证无效

Using jQuery Validate with Select2 for validation not working

我有一个包含 SELECT2 的表格。

示例图片如下:

正如您在图片中看到的,错误标签与其他错误标签不同。

有两个问题:

  1. 如何修复 SELECT2 上的错误标签,使其与其他错误标签相同?
  2. 当 SELECT2 更改其值时,如何添加或删除验证? (对不起,我是 SELECT2 的新手)

这是我验证表单的代码:

$("#systemcodeForm").validate({
    submitHandler: function (form) {
      
  },
  rules: {
    systemtype: {
      required: true         
    }
  },
  messages: {
    systemtype: {
      required: "Please choose the system type",
    }
  },
  errorPlacement: function(label, element) {
    if(element.hasClass('web-select2') && element.next('.select2-container').length) {
      label.insertAfter(element.next('.select2-container'));
    }
    else{
      label.addClass('mt-2 text-danger');
      label.insertAfter(element);
    }
  },
  highlight: function(element) {
    $(element).parent().addClass('has-danger')
    $(element).addClass('form-control-danger')
  },
  success: function(label,element) {
    $(element).parent().removeClass('has-danger')
    $(element).removeClass('form-control-danger')
    label.remove();
  }
});

这是我的 HTML 代码:

<form class="cmxform" id="systemrightsForm" method="post" action="#">
 <div class="row">
   <div class="col-md-12">
      <div class="form-group">
        <label class="control-label">System Type</label>
        <select id="systemtype" name="systemtype" class="web-select2 form-control" data-width="100%">
            <option value="">-- SELECT SYSTEM TYPE --</option>
            <option value="1">Option 1</option>
        </select>
      </div>
    </div>
</div>
<div class="row">
   <div class="col-md-12">
    <div class="form-group">
      <label class="control-label" for="systemcode">System Code</label>
      <input type="text" class="form-control" id="systemcode" name="systemcode" placeholder="Enter system code" autocomplete="off" maxlength="15">
     </div>
   </div>
</div>
<div class="row">
  <div class="col-md-12">
   <div class="form-group">
     <label class="control-label" for="systemdesc">Description</label>
        <input type="text" class="form-control" id="systemdesc" name="systemdesc" placeholder="Enter system description" autocomplete="off" maxlength="40">
    </div>
  </div>
</div>
</form>

您可以简单地使用本机 select2 change 函数来监视 select 选项 selection 上的 change

因为您正在使用 jQuery .validate 来 validate 输入 select 所以默认情况下 .validate 不会在 option 之后隐藏标签 selected.

为了获得正确的label我们需要使用global variable来存储标签元素然后remove那个label在select option.

的离子

现场工作演示:(两个问题都已解决)

var mySelect2 = $('#systemtype')

//initiate select
mySelect2.select2();

//global var for select 2 label
var select2label

$("#systemrightsForm").validate({

  rules: {
    systemtype: {
      required: true
    },
    systemcode: {
      required: true
    },
    systemdesc: {
      required: true
    }
  },
  messages: {
    systemtype: {
      required: "Please choose the system type",
    },

  },
  errorPlacement: function(label, element) {
    if (element.hasClass('web-select2')) {
      label.insertAfter(element.next('.select2-container')).addClass('mt-2 text-danger');
      select2label = label
    } else {
      label.addClass('mt-2 text-danger');
      label.insertAfter(element);
    }
  },
  highlight: function(element) {
    $(element).parent().addClass('is-invalid')
    $(element).addClass('form-control-danger')
  },
  success: function(label, element) {
    $(element).parent().removeClass('is-invalid')
    $(element).removeClass('form-control-danger')
    label.remove();
  },
  submitHandler: function(form) {

  },
});

//watch the change on select
mySelect2.on("change", function(e) {
  select2label.remove(); //remove label
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<form class="cmxform" id="systemrightsForm">
  <div class="row">
    <div class="col-md-12">
      <div class="form-group">
        <label class="control-label">System Type</label>
        <select id="systemtype" name="systemtype" class="web-select2 form-control" data-width="100%">
          <option value="">-- SELECT SYSTEM TYPE --</option>
          <option value="1">Option 1</option>
        </select>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <div class="form-group">
        <label class="control-label" for="systemcode">System Code</label>
        <input type="text" class="form-control" id="systemcode" name="systemcode" placeholder="Enter system code" autocomplete="off" maxlength="15">
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <div class="form-group">
        <label class="control-label" for="systemdesc">Description</label>
        <input type="text" class="form-control" id="systemdesc" name="systemdesc" placeholder="Enter system description" autocomplete="off" maxlength="40">
      </div>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>

</form>