jQuery验证未捕获的TypeError无法读取未定义的属性"form" [英] jQuery validate Uncaught TypeError Cannot read property 'form' of undefined

查看:137
本文介绍了jQuery验证未捕获的TypeError无法读取未定义的属性"form"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery validate http://jqueryvalidation.org/

I am using jquery validate http://jqueryvalidation.org/

我有一个表单,其中的第一个字段是手动添加的(硬编码)

I have a form with a first field added manually (hard coded)

然后以编程方式添加更多字段. ARE已经在表单标签内附加到div#to-append

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

});

但是出现上述未捕获的TypeError无法读取未定义的属性'form'"错误.

But get the "Uncaught TypeError Cannot read property 'form' of undefined" error stated above.

任何帮助将不胜感激.

推荐答案

您要添加ID字符串规则,而不是元素选择器,然后将规则添加到该选择器.

You 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', {
       requzired: true,
       minlength: 3,
       pattern: yt
    });
});

使用$('#'+ $ largestID)获取该ID的字段,然后添加规则.

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,
      }
    }
});

这篇关于jQuery验证未捕获的TypeError无法读取未定义的属性"form"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆