简体   繁体   English

jQuery验证-无法调用未定义的方法“调用”

[英]jQuery Validation - Cannot call method 'call' of undefined

Not sure why this isn't working. 不知道为什么这不起作用。 I'm sure you all know what I'm trying to achieve here. 我相信你们都知道我在这里想要达到的目标。

When the script is run I get an Uncaught TypeError: undefined is not a function on jquery.validate.js line:366 which refers to this hunk of code: 运行脚本时,我收到Uncaught TypeError: undefined is not a function jquery.validate.js line:366上的Uncaught TypeError: undefined is not a function ,它引用了以下代码:

function delegate( event ) {
            var validator = $.data( this[ 0 ].form, "validator" ),
                eventType = "on" + event.type.replace( /^validate/, "" ),
                settings = validator.settings;
            if ( settings[ eventType ] && !this.is( settings.ignore ) ) {
                settings[ eventType ].call( validator, this[ 0 ], event );
            }
        }                           ^^^^ HERE

I'm using codeigniter2 framework so apologies if my form page doesn't make sense to some. 我正在使用codeigniter2框架,因此如果我的表单页面对某些人没有意义,则表示歉意。

new_page.php (The relevant bit) new_page.php(相关位)

<?php echo form_open(site_url('admin/manage_pages/new_page'),
                            array('id' => 'new-page-form')) . PHP_EOL; ?>
    <div class="form-group">
        <?php echo form_label('Page Title', 'page-title',
                                'class="control-label"') . PHP_EOL; ?>
        <?php echo form_input(array(
                                'name' => 'page-title',
                                'id' => 'input-page-title',
                                'max-length' => '255',
                                'placeholder' => 'Enter Page Title',
                                'class' => 'form-control')) . PHP_EOL; ?>
    </div><!-- /.form-group -->
    <div class="form-group">
        <?php echo form_label('Url', 'slug', 'class="control-label"'); ?>
        <span id="input-slug-span" class="form-control">
            <span id="input-slug-static"><?php echo site_url(); ?></span>
            <?php echo form_input(array(
                                    'name' => 'slug',
                                    'id' => 'input-slug')); ?>
        </span>
    </div><!-- /.form-group -->

and in new_page.validation.js 并在new_page.validation.js中

$(document).ready(function() {

    //--------------------------------------------------------------------------------------
    //
    //  New Page Client Side Validation
    //
    //--------------------------------------------------------------------------------------

    $('#new-page-form').validate({

        onkeyup:true,

        rules: {
            'page-title': {
                required: true,
                remote: { 
                    url: 'new_title_exists',
                    type: 'post',
                    data: {
                        'page-title': function() { return $("#input-page-title").val(); }
                    }
                }
            },
            'slug': {
                required: true,
                remote: {
                    url: 'new_slug_exists',
                    type: 'post',
                    data: {
                        'slug': function() { return $('#input-slug').val(); }
                    }
                }
            }
        },
        messages: {
            'page-title': {
                required: 'Page Title is required.',
                remote: 'Page Title must be unique.'
            },
            'slug': {
                required: 'A URL slug is required',
                remote: 'The URL must be unique.'
            }
        }
    })
})

Any help would be grateful! 任何帮助将不胜感激! Cheers guys 欢呼的家伙

$('#new-page-form').validate({
    // onkeyup:true, // REMOVE THIS
    rules: {
        'page-title': {
            required: true,
            remote: {  // element's value IS always sent
                url: 'new_title_exists',
                type: 'post',
                data: {  // MUST send the CSRF Token Value
                    'csrftoken': function() {
                        return $('input[name="csrftoken"]').val();
                    }
                }
            }
        },
        ....
  • onkeyup: true could potentially break the normal functioning of the plugin. onkeyup: true可能会破坏插件的正常运行。 As per documentation, "a boolean true is not a valid value" . 根据文档, “布尔true为无效值” That's because the onkeyup ability is already the default. 这是因为onkeyup功能已经是默认功能。

  • Whenever using jQuery Validate remote method, or any ajax() , in a CodeIgniter project, you also have to send the CSRF token within the data , otherwise you will get a 500 server error. 每当在CodeIgniter项目中使用jQuery Validate remote方法或任何ajax() ,您还必须在data内发送CSRF令牌,否则将收到500服务器错误。 You can pick this up from the hidden input element on your form. 您可以从表单上的隐藏输入元素中进行选择。 You do not need to send the value of the input element being evaluated, however, because that data is already being sent by default. 但是,您不需要发送要求值的输入元素的值,因为默认情况下已经发送了该数据。 Change the name above to match what's showing in the rendered source code of your form. 更改上面的name以匹配表单呈现的源代码中显示的内容。

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

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