同一個頁面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 關注
- 216 瀏覽
添加回答
舉報
0/150
提交
取消