jQuery验证 - 无法调用未定义的方法'呼吁'未定义、方法、jQuery

由网友(莫气少年少.)分享简介:不知道为什么,这是行不通的。我相信你们都知道我想要在这里实现。在运行该脚本,我得到一个未捕获的类型错误:未定义不上 jquery.validate.js行功能:366 是指这个大块头的code:函数委托(事件){VAR验证= $ .DATA(此[0] .FORM,验证),EVENTTYPE上的+ event.type...

不知道为什么,这是行不通的。 我相信你们都知道我想要在这里实现。

在运行该脚本,我得到一个未捕获的类型错误:未定义不上 jquery.validate.js行功能:366 是指这个大块头的code:

 函数委托(事件){
            VAR验证= $ .DATA(此[0] .FORM,验证),
                EVENTTYPE上的+ event.type.replace(/ ^验证/,)=,
                设置= validator.settings;
            如果(设置[EVENTTYPE]放大器;&安培;!this.is(settings.ignore)){
                设置[EVENTTYPE] .CALL(验证,这[0],事件);
            }
        } ^^^^这里
 

我用codeigniter2框架,以便道歉,如果我的表单页面没有意义一些。

new_page.php(相关位)

 <?PHP的回声form_open(SITE_URL(管理/ manage_pages / new_page),
                            阵列('身份证'=>新网页形式'))。 PHP_EOL; ?>
    < D​​IV CLASS =形组>
        <?PHP的回声form_label(页面标题,网页标题,
                                类=控制标签)。 PHP_EOL; ?>
        <?PHP的回声form_input(阵列(
                                '名'=>页面标题,
                                'ID'=> 输入页面标题,
                                最大长度=> '255',
                                '占位'=> 输入页面标题,
                                类=> 表格控))。 PHP_EOL; ?>
    < / DIV><! -  /.form-group  - >
    < D​​IV CLASS =形组>
        <?PHP的回声form_label('地址','塞','级=控制标签'); ?>
        <跨度ID =输入塞跨度级=表单控制>
            <跨度ID =输入塞静电>< PHP的回声SITE_URL(); ?>< / SPAN>
            <?PHP的回声form_input(阵列(
                                    '名'=> 塞,
                                    'ID'=> 输入蛞蝓')); ?>
        < / SPAN>
    < / DIV><! -  /.form-group  - >
 

和在new_page.validation.js

  $(文件)。就绪(函数(){

    // ------------------------------------------------ --------------------------------------
    //
    //新页客户端验证
    //
    // ------------------------------------------------ --------------------------------------

    $('#新页的形式')。验证({

        的onkeyup:真正的,

        规则:{
            页面标题:{
                要求:真实,
                远程: {
                    网址:new_title_exists,
                    类型:'后',
                    数据: {
                        页面标题:函数(){返回$(#输入页面标题)VAL()。 }
                    }
                }
            },
            塞:{
                要求:真实,
                远程: {
                    网址:new_slug_exists,
                    类型:'后',
                    数据: {
                        塞:函数(){返回$('#输入塞)VAL()。 }
                    }
                }
            }
        },
        消息:{
            页面标题:{
                要求:页面标题是必需的。,
                遥控器:页面标题必须是唯一的。
            },
            塞:{
                要求:一个URL塞是必需的,
                遥控器:这个URL必须是唯一的。
            }
        }
    })
})
 

任何帮助将不胜感激!干杯家伙

解决方案

  $('#新页的形式')。验证({
    //的onkeyup:真正的,//删除此
    规则:{
        页面标题:{
            要求:真实,
            遥控器:{//元素的值始终发送
                网址:new_title_exists,
                类型:'后',
                数据:{//务必发送CSRF令牌值
                    csrftoken:函数(){
                        返回$('输入[名称=csrftoken])VAL()。
                    }
                }
            }
        },
        ....
 

的onkeyup:真正的有可能打破插件的正常运作。 根据文档,一个true不是一个有效的值的。这是因为的onkeyup 的能力已经是默认的。

每当使用jQuery验证远程方法,或任何阿贾克斯(),在codeIgniter项目,您还可以发送数据中的CSRF令牌,否则你将得到一个500服务器错误。您可以从您的窗体上的隐藏输入元素选择这个了。不需要发送但是输入元素的值被评估,因为该数据已经被发送的默认。更改名称以上,以配合什么的呈现在你的形式呈现的源$ C ​​$ C。

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:

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.

new_page.php (The relevant bit)

<?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

$(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. As per documentation, "a boolean true is not a valid value". That's because the onkeyup ability is already the default.

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. 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.

阅读全文

相关推荐

最新文章