jquery.validate.js校验select2解决方案

Jquery插件select2校验解决方案

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年9月15日 11:25:26 星期二

http://fanshuyao.iteye.com/

为了用jquery.validate.js校验select2,折腾了2天,现在终于解决了,把方法告诉大家。

一、使用用了select2美化select

$('select').select2();

二、页面部分代码

<div class="form-group">
<label for="tel1" class="control-label">性别:</label>
<div>
<select name="genders" id="genders" class="form-control">
<option value="">请选择…</option>
<c:forEach items="${genders}" var="gender">
<option value="${gender}" ${userInSession.genders eq gender?"selected='selected'":""}>${gender.value}</option>
</c:forEach>
</select>
</div>
</div>

三、使用jquery.validate.js

导入js文件

jquery.validate.min.js

additional-methods.min.js

messages_zh.min.js

additional-methods-custom.js  (自己定义的校验方法扩展js文件)

四、校验

var validateObj = $('#userInfoEditForm').validate({
ignore: "",
errorClass : 'help-block',
focusInvalid : true,
rules : {
genders : {
required : true
}
},
messages : {
genders : {
required : "请选择性别"
}
submitHandler : function(form) {
return false;
form.submit();//form.submit(); 或者$(form).ajaxSubmit();
}
}); $("#genders").change(function(){
$(this).valid();
});

五、问题说明

1、使用select2美化select下拉列表后,select2会把原来的select隐藏掉,设置css属性(display:none),然后再通过select2重新定制界面

2、但jquery.validate.js默认不是校验:hidden属性的控件,所以造成原来的select校验不了

官网说明:

ignore (default: ":hidden")

Type: Selector

Elements to ignore when validating, simply filtering them out. jQuery's not-method is used, therefore everything that is accepted by not() can be passed as this option. Inputs of type submit and reset are always ignored, so are disabled elements.

Example: Ignores all elements with the class "ignore" when validating.

$("#myform").validate({

ignore: ".ignore"

});

3、解决方法就是加ignore属性,把值设置为""空字符串

ignore: "",

4、设置ignore后在提交时就会对隐藏的属性进行校验,但选择的时候没有进行实时校验,因为你选择的是select2。所以要给select加一个change事件,当改变时,再校验一次。

$("#genders").change(function(){
$(this).valid();
});

需要注意的是:valid方法是jquery.validate.js中的方法,所以用的时候,要先调用初始化方法validate( ),

然后在后面再重新调用valid()校验方法。

valid( )  可以校验整个表单,也可以校验单个属性。

官网说明:

valid( )

Returns: Boolean

Description: Checks whether the selected form is valid or whether all selected elements are valid.

validate() needs to be called on the form before checking it using this method.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2015年9月15日 11:25:26 星期二

http://fanshuyao.iteye.com/

最新文章

  1. LeetCode之171. Excel Sheet Column Number
  2. win7系统部分便笺的元数据已被损坏怎么恢复
  3. 在linux中添加ftp用户,并设置相应的权限
  4. 【Bootstrap】1.初识Bootstrap
  5. 又发现个.net framework的坑
  6. AngularJS引入Echarts的Demo
  7. COM编程之四 引用计数
  8. DB2 SQL性能调优秘笈
  9. windowsUI的总结
  10. LA 5009 (三分法求极值) Error Curves
  11. HDOJ-ACM1012(JAVA)
  12. 深入理解Javascript之this关键字
  13. 关于c:\fakepath\的解决办法
  14. BZOJ_4128_Matrix_矩阵乘法+哈希+BSGS
  15. Go语言之高级篇beego框架之layui框架应用
  16. error: Microsoft Visual C++ 14.0 is required(line_profiler模块安装失败的解决办法)
  17. 原来CNN是这样提取图像特征的。。。
  18. 什么是lambda函数,它有什么好处
  19. iOS开发-UIScrollView图片无限循环
  20. Selenium (4) —— Selenium是什么? WebDriver是什么?做什么?(101 Tutorial)

热门文章

  1. block的是发送信号的线程,又不是处理槽函数的线程
  2. 让你的短信应用迎接Android 4.4(KitKat)
  3. eclipse导入已有源码
  4. 《深入理解linux内核架构》第二章 进程管理和调度
  5. Android日期时间格式国际化
  6. VirtualBox的四种网络连接方式详解
  7. 是否存在两个树的和是固定数 hashmap使用 leecode
  8. 2013 ACM区域赛长沙 C Collision HDU 4793
  9. Tyvj P1463 智商问题 分块
  10. Linux I2C设备驱动编写(三)-实例分析AM3359