admin管理员组文章数量:1287849
I am using jquery validate /
I have a form with a first field added manually (hard coded)
Then adding more fields is done programatically. The ARE appended inside the form tags already, into a div #to-append
The one added manually validates fine.
For the rest, I am trying to add the rules like this, every time i add a field:
var $largestID = $("#to-append > div:last > div:first > input:first").attr("id");
console.log($largestID); //throws right id
$($largestID).rules("add", {
required: true,
minlength: 3,
pattern: yt
});
But get the "Uncaught TypeError Cannot read property 'form' of undefined" error stated above.
Any help would be appreciated.
I am using jquery validate http://jqueryvalidation/
I have a form with a first field added manually (hard coded)
Then adding more fields is done programatically. The ARE appended inside the form tags already, into a div #to-append
The one added manually validates fine.
For the rest, I am trying to add the rules like this, every time i add a field:
var $largestID = $("#to-append > div:last > div:first > input:first").attr("id");
console.log($largestID); //throws right id
$($largestID).rules("add", {
required: true,
minlength: 3,
pattern: yt
});
But get the "Uncaught TypeError Cannot read property 'form' of undefined" error stated above.
Any help would be appreciated.
Share Improve this question asked Feb 18, 2016 at 5:46 user3808307user3808307 1,47112 gold badges63 silver badges113 bronze badges 6- The rules should be added to the DOM elements or the tags. You are adding to ID. – Sravan Commented Feb 18, 2016 at 5:56
- What is the place of this js code in your script, before form or after form? – PHPExpert Commented Feb 18, 2016 at 5:59
- @Sravan I tried with name it still doesn't work – user3808307 Commented Feb 18, 2016 at 6:03
- @ PHPExpert I am including it at the end before the </body> – user3808307 Commented Feb 18, 2016 at 6:04
- @Sravan I get a different error: Uncaught Error: Syntax error, unrecognized expression: url[] . My names are an array, how can i do it then? – user3808307 Commented Feb 18, 2016 at 6:20
2 Answers
Reset to default 4You are adding rule taking ID string. Instead take the element selector, and add rule to that selector.
var $largestID = $("#to-append > div:last > div:first > input:first").attr("id");
console.log($largestID); //throws right id
$('#'+$largestID).each(function () {
$(this).rules('add', {
required: true,
minlength: 3,
pattern: yt
});
});
Use the $('#'+$largestID)
to get the field of that ID, and then add rules.
To add rules to array, use:
$('#'+$largestID).each(function () { })
you can even validate the array of names of that element:
$('#'+$largestID).validate({
rules: {
'url[]': {
required: true,
}
}
});
There's no need to use the ID. You have a selector that returns the element, add the rule to that directly.
$("#to-append > div:last > div:first > input:first").rules("add", {
required: true,
minlength: 3,
pattern: yt
});
版权声明:本文标题:javascript - jQuery validate Uncaught TypeError Cannot read property 'form' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741320363a2372169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论