您的位置:首页 > Web前端 > JQuery

jQuery validate 组件使用技巧

2014-07-18 00:00 113 查看
写法:<input type='text' name='account' class = "{validate: { 'required' : '#custom:checked' } } />"
根据单选按钮#custom的当前选择状态( #custom:checked 的返回值 )确定文本输入框是否为必填字段。

element 方法使用
验证单个元素是否有效。
$( "#myForm" ).validate().element("#mySelect");

validate在ie里无效,form(), valid() 方法均返回true
原因:validator的element方法在ie里无法获当前表单的组件列表。
解决方法:将element方法中的
return $([]).add(this.currentForm.elements).filter(":input")
替换为:
var listEle=new Array();
for ( var k=0; k<this.currentForm.elements.length; k++){
listEle[k] = this.currentForm.elements[k];
}


addMethod( ) 的中常用的optional( ) 方法
jQuery.validate的optional(element),用于表单控件的值不为空时才触发验证。当表单为空时返回true,
例如:
jQuery.validator.addMethod("mobile", element){
var reg=/^[0-9]+$/;
return this.optional(element) || reg.test(value);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: