且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery验证未捕获的TypeError无法读取未定义的属性"form"

更新时间:2022-10-28 11:55:16

您要添加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,
      }
    }
});