select2.js和Bootstrap验证器无法正常工作

时间:2015-06-14 15:01:17

标签: javascript jquery twitter-bootstrap

我无法弄清楚为什么select2.js和Bootstrap验证器无法正常工作。

jsfiddle

$('form.validate-form').validator({
    disable: false
});

$(".mySelect").select2();

$( "select.mySelect" ).each(function() {
    $(this).validator();
    $(this).get(0).setCustomValidity('Please select at least one item in the list.');
});

1 个答案:

答案 0 :(得分:0)

您必须为表单和id添加规则以选择2来验证它

  

ID = “mySelect”

表单html和脚本

<form class="validate-form" data-toggle="validator"> 
   <label class="required">Geographic market experience </label>
   <select id="mySelect" multiple="true" class="mySelect">
      <option value="AL">Alabama</option>
      <option value="Am">Amalapuram</option>
      <option value="An">Anakapalli</option>
      <option value="Ak">Akkayapalem</option>
      <option value="WY">Wyoming</option>
   </select>
   <div class="help-block with-errors"></div>
   <button type="submit">Submit</button>
</form>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script type="text/javascript">
$("form.validate-form").validate({
  rules: {
      mySelect: "required"
  },
  messages: {
      mySelect: "Please select your region"
  }
});
</script>