简体   繁体   English

将jQuery验证规则添加到动态生成的表单元素时出错

[英]Error adding jQuery Validation rule to dynamically generated form element

I am developing a website using the Laravel 4 php framework, and I have a page which displays a form that is validated using the jQuery Validation plugin. 我正在使用Laravel 4 php框架开发网站,并且有一个页面显示使用jQuery Validation插件验证的表单。

Clicking a button in the form adds several fields to the form with a jQuery AJAX request via the .load() method. 单击表单中的按钮,通过.load()方法使用jQuery AJAX请求向表单中添加多个字段。 Once I've added the fields to the form, I want to add validation rules for these fields, which I do using the .rules() method. 将字段添加到表单后,我想使用.rules()方法添加这些字段的验证规则。

However, when my javascript is called, I get the following error: 但是,当我的JavaScript被调用时,出现以下错误:

Uncaught TypeError: Cannot read property 'form' of undefined jquery.validate.min.js:2 未捕获的TypeError:无法读取未定义的jquery.validate.min.js:2的属性“ form”

As a test, I appended an element to the appropriate div and called .rules() on it, and it validated as expected, so it seems that for some reason I'm getting an error because my form elements are being generated using php and then added to the page using .load() . 作为测试,我在适当的div附加了一个元素,并在其上调用了.rules() ,并且它按预期进行了验证,因此由于某种原因,由于使用php和然后使用.load()添加到页面。

Here's my code: 这是我的代码:

The page in question: 有问题的页面:

<?php echo Form::open(array('id' => 'researcher-info-form')) ?>
<?php foreach($researcher_template as $slug => $title): ?>
    <tr>
        <td></td>
        <td>
            <?php echo Form::label('researcher_' . $slug, $title); ?>
            <?php echo Form::text('researcher_' . $slug); ?>
        </td>
        <td class="validation-error-message"></td>
        </tr>
<?php endforeach; ?>
...
<div id="billing-address-wrapper">
    <?php // a jQuery event loads form fields into this div dynamically. ?>
</div>
...
<?php Form::close(); ?>
...

The javascript that handles this is: 处理此问题的javascript是:

jQuery(document).ready(function($) {
    $("form").validate({
        submitHandler: submitSeqRequest,
        rules: {
        researcher_name: "required",
        researcher_email: {
            required: true,
            email: true
            },
        pi_name: "required",
        template: "required",
        primer: "required",
        dna_type: "required",
        bases_needed: "required"
        },
        errorPlacement: function(error, element) {
            error.appendTo( element.parent("td").next("td") );
        }
    });
    addLabAddress();
});

function addLabAddress() {
    $('#add-my-group').click( function() {
        $('#billing-address-wrapper').load("/order/enterLabAddress");
        $('#group_name').rules("add", {
            required: true
        })
    });
    return false;
}

And the partial view located at /order/enterLabAddress is: 并且位于/order/enterLabAddress的局部视图为:

<?php $billing_template = (new Lab)->billing_template; ?>
<?php foreach($billing_template as $slug => $title): ?>
    <div>
        <?php echo Form::label($slug, $title); ?>
        <?php echo Form::text($slug);   ?>
    </div>
<?php endforeach; ?>

Can anyone help? 有人可以帮忙吗? Why am I getting this error using .load() ? 为什么使用.load()出现此错误? and not .append() ? 而不是.append()

When you call in data using .load() / AJAX, the validating JS script has no clue that those new elements exist. 当您使用.load()/ AJAX调用数据时,验证的JS脚本不知道这些新元素是否存在。

Notice that your validating setup is done when the page initially loads 请注意,您的验证设置是在页面最初加载时完成的

jQuery(document).ready(function($) {

You need to run the contents of that ready(...) function after each new .load() so the validator becomes away of new DOM elements. 您需要在每个新的.load()之后运行该ready(...)函数的内容,以使验证程序脱离新的DOM元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM