<form name="form" id="form1"> <table> <tr> <td>类型:</td> <td> <select name="type" id="type"> <option value="">请选择</option> <option value="1">身份证</option> <option value="2">护照</option> </select> </td> </tr> <tr> <td>号码:</td> <td><input type="text" name="code" id="code"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form>
$(function(){ /***** add/remove rule*****/ $("#type").change(function(){ $("#code").rules("remove"); if($(this).val()=="1"){ $("#code").rules("add",{required:true,minlength:6,messages:{required:"请输入正确身份号"}}); }else if($(this).val()=="2"){ $("#code").rules("add",{required:true,minlength:3,messages:{required:"请输入正确护照号"}}); } }); $("#form1").validate({ errorPlacement: function (lable, element) { lable.appendTo(element.parent()); }, success: function (lable) { lable.remove(); }, submitHandler: function (form) { console.log("form submit"); } }); });