weixin_33708432 2015-11-24 07:51 采纳率: 0%
浏览 98

TypeError:不是函数

I'm building a simple script. I'm trying to not use anonymous functions in jQuery, in order to maintain clean the code. This is the code:

jQuery(function($) {
    'use strict';
    var Call = {
        selection: '',
        init: function() {
            this.product_opt = $('#product_opt');
            this.listenChange();
            //this.test();
        },
        listenChange: function() {
            this.product_opt.live('change', this.afterChange);
        },
        afterChange: function() {
            $("#product_opt :checked").each(function() {
                this.selection = $(this).attr('value');
                console.log( this.selection );
                this.ajax_get_cat();
            });
        },
        ajax_get_cat : function(){
            return $.ajax({
                url: 'http://localhost:8888/mydomain/wp-admin/admin-ajax.php',
                data: {
                    'action': 'show_slider_callback',
                    'selection': this.selection
                },
                success: function(data) {
                    // This outputs the result of the ajax request
                    //console.log(data);
                    console.log('returned' + data);
                },
                error: function(errorThrown) {
                    console.log(errorThrown);
                }
            });
        }
    };

    Call.init();
});

And this is the HTML:

<div id="product_opt">
    <input id="1" class="selector" type="radio" name="group1" value="6" />  
    <input id="1" class="selector" type="radio" name="group1" value="6" />    </div>

When I try to use the form this is the error message returned in console:

TypeError: this.ajax_get_cat is not a function this.ajax_get_cat();

  • 写回答

3条回答 默认 最新

  • weixin_33692284 2015-11-24 07:56
    关注

    Your problem here is you're using this.ajax_get_cat within your loop where this actually references each element returned in the selector.

    You're also making the same mistake when setting this.selection, simply change this to Call and you should be good to go.

    jQuery(function($) {
        'use strict';
        var Call = {
            selection: '',
            init: function() {
                this.product_opt = $('#product_opt');
                this.listenChange();
                //this.test();
            },
            listenChange: function() {
                this.product_opt.live('change', this.afterChange);
            },
            afterChange: function() {
                $("#product_opt :checked").each(function() {
                    Call.selection = $(this).attr('value');
                    console.log( Call.selection );
                    Call.ajax_get_cat();
                });
            },
            ajax_get_cat : function(){
                return $.ajax({
                    url: 'http://localhost:8888/mydomain/wp-admin/admin-ajax.php',
                    data: {
                        'action': 'show_slider_callback',
                        'selection': this.selection
                    },
                    success: function(data) {
                        // This outputs the result of the ajax request
                        //console.log(data);
                        console.log('returned' + data);
                    },
                    error: function(errorThrown) {
                        console.log(errorThrown);
                    }
                });
            }
        };
    
        Call.init();
    });
    

    You can also store a proper reference to this before your loop like so:

    jQuery(function($) {
        'use strict';
        var Call = {
            selection: '',
            init: function() {
                this.product_opt = $('#product_opt');
                this.listenChange();
                //this.test();
            },
            listenChange: function() {
                this.product_opt.live('change', this.afterChange);
            },
            afterChange: function() {
                var _this = this;
                $("#product_opt :checked").each(function() {
                    _this.selection = $(this).attr('value');
                    console.log( _this.selection );
                    _this.ajax_get_cat();
                });
            },
            ajax_get_cat : function(){
                return $.ajax({
                    url: 'http://localhost:8888/mydomain/wp-admin/admin-ajax.php',
                    data: {
                        'action': 'show_slider_callback',
                        'selection': this.selection
                    },
                    success: function(data) {
                        // This outputs the result of the ajax request
                        //console.log(data);
                        console.log('returned' + data);
                    },
                    error: function(errorThrown) {
                        console.log(errorThrown);
                    }
                });
            }
        };
    
        Call.init();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度