select2 and jquery validation not working properly

若如初见. 提交于 2019-11-27 16:22:43

By default, the validation on a field is only triggered by these events (for a type="text" field)...

  • onfocusout, when the user leaves a field (validates just the field)

  • onkeyup, when the user is typing within a field (validates just the field)

  • onclick, when the user clicks the submit button (validates the whole form)

If you need to programmatically trigger validation after you programmatically change the value of a field, then you need to invoke the .valid() method on that field. It will effectively trigger validation similar as the events above.

$('#myField').valid();  // validates just field element with id="myField"

$('#myForm').valid();  // validates the whole form

So you'll need to find a method within select2 that is fired whenever the value is changed and put .valid() inside of that. It looks like the change event is what you'll need.

$("#vedit-filter").select2({
    // your options here
}).on('change', function() {
    $(this).valid();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!