我有一个表单,我使用jquery.validate.我最初调用validate与一组规则和自定义消息…
$("#formName").validate( { rules: { myExistingInput: { required: true } },messages: { myExistingInput: { required: "Enter something" } },ignore: null,// include hidden fields (see below) submitHandler: function(form) { // do stuff },invalidHandler: function(event,validator) { // do stuff (some of the fields may have been hidden by a collapsible panel // if there is an error on one of those fields,expand the panel so the error // becomes visible) } });
$("#formName").append(...); $("#newInputName").rules("add",{ required: true,messages: { required: "Enter something else" } });
如果我提交表单,我从jquery.validate中得到一个错误…
Exception occured when checking element newInputName,check the
‘messages’ method.TypeError: Unable to get property ‘call’ of
undefined or null reference
在浏览器中调试,我可以看到从“check”函数中抛出的错误,“method”变量设置为“messages”.
$("#newInputName").rules("add",{ required: true });
我在这里看到很多例子,说明我的语法是正确的.有什么建议么?
BTW:jQuery验证插件 – v1.11.0 – 2/4/2013
@H_301_38@解决方法
你的代码似乎正在工作,没有错误,你发布它.
DEMO与DOM准备:http://jsfiddle.net/UZTnE/
DEMO与PageInit& jQuery手机:http://jsfiddle.net/xJ3E2/
$(document).on("pageinit",function () { $('#myform').validate({ // initialize the plugin rules: { field1: { required: true } },messages: { field1: { required: "Enter something" } } }); $('[name*="field"]').each(function () { $(this).rules('add',{ required: true,messages: { required: "Enter something else" } }); }); });
HTML:
<form id="myform"> <input type="text" name="field1" /> <input type="text" name="field2" /> <input type="submit" /> </form>
BTW:
这个…
ignore: null,// include hidden fields
应该…
ignore: [],// include hidden fields
见:jQuery Validate – Enable validation for hidden fields
@H_301_38@ @H_301_38@