同一个页面a.php分别请求a.php?t=1;a.php?t=2;我使用jQuery Validate 来js判断文本域的有效性和数据提交.问在同-个页面a.php上,有一个文本域someinput,如何在以上两次不同的请求后,提交动态设置文本域的rule,使t=1需要判断,t=2不需要判断?
1 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
其实除了可以用js调整,也可以用php调整啊,根据$_GET['t']的值输出不同的rule.
特意去看了下jQuery Validate插件,发现可以这样这么做:
<form id="myform"> <input name="someinput" id="someinput" type="text"> <button>submit</button></form>
javascript:
$('#myform').validate({rules:{
someinput:{ <?php if ($_GET['t']==1) :?>
required:true,
minlength:3,
maxlength:15,
number:true <?php else :?>
required:true,
minlength:1,
maxlength:5,
number:true <?php endif;?>
}
}});或者这么做:
function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split('&'); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('='); if (decodeURIComponent(pair[0]) == variable) { return decodeURIComponent(pair[1]);
}
}
}
$(function(){var t = getQueryVariable('t');
$.validator.addMethod("someinput", function(value) { if(t==1){ return !(value===null || value.length < 2 || value.length > 20);
}else if(t==2){ return !(value===null || value.length < 5 || value.length > 12);
} return false;
}, 'Please enter "someinput"!');
$('#myform').validate({rules:{ someinput:'someinput'}});
});- 1 回答
- 0 关注
- 283 浏览
添加回答
举报
0/150
提交
取消
