代码之家  ›  专栏  ›  技术社区  ›  Ben

变量范围帮助:这个。重置()不是函数

  •  1
  • Ben  · 技术社区  · 14 年前

    执行save()时这个。重置()或那。重置()它找不到reset()方法,并且说它不是函数。我在init()上使用了一个变通方法来让它工作,但是这个方法在save()中不起作用

    var vehicle = function () {
        return {
            init: function () {
                var that = this;
    
                jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) {
                    e.preventDefault();
                    that.remove(jQuery(e.currentTarget).parents('.vehicle-year-profile'));
                });
    
                jQuery('.vehicle-year-profile .options .edit').bind('click', function (e) {
                    e.preventDefault();
                    that.edit(jQuery(e.currentTarget).parents('.vehicle-year-profile').attr('id'));
                });
    
                jQuery('#association-detail .save').bind('click', function (e) {
                    e.preventDefault();
                    that.save();
                });
            },
            save: function () {
                var data = new Array();
                data['onSet'] = '';
                var onSet = jQuery('#association-detail input:checked');
                for (var i = 0; i < (onSet.length-1); i++) {
                    data['onSet'] = data['onSet']+','+onSet.attr('id');
                }
    
                var priceSet = jQuery('#association-detail input[type=text]');
                for (var i = 0; i < (priceSet.length-1); i++) {
                    data['priceSet'] = data['priceSet']+','+priceSet.attr('id')+':'+priceSet.val();
                }
    
                jQuery.ajax({
                    type: 'post',
                    url: '/ajax/store/product/saveAssocDetail.php',
                    data: data,
                        success: function (r) {
                        if (r.length > 0) {
                            document.triggerNotification('check', 'Changes have been saved');
                            var that = this;
                            that.reset(); //ERROR IS TRIGGERED HERE
                        } else {
                            document.triggerNotification('x', 'Unable to save changes');
                        }
                        },
                    error: function () {
                        document.triggerNotification('x', 'Unable to process your request, ajax file not found');
                        return false;
                    }
                });
            },
            reset: function () {
                jQuery('#association-detail h3').html('');
                jQuery('#assocationVehicleId').val('');
    
                jQuery('#association-detail input:checked').attr('checked', false);
                jQuery('#association-detail input[type=text]').val('0.00');
    
                jQuery('#association-detail').hide();
            }
        }
    }();
    jQuery(function() {
        vehicle.init();
    });
    
    2 回复  |  直到 14 年前
        1
  •  6
  •   treeface    14 年前

    也许是因为你提到了 this

    var that = this;
    

    在进行ajax调用之前,然后显式地引用 that

    var that = this;
    
    jQuery.ajax({
        type: 'post',
        url: '/ajax/store/product/saveAssocDetail.php',
        data: data,
        success: function (r) {
            if (r.length > 0) {
                document.triggerNotification('check', 'Changes have been saved');
    
                /**
                * now you can refer to "that", which is in the proper scope
                */
    
                that.reset(); //ERROR IS TRIGGERED HERE
            } else {
                document.triggerNotification('x', 'Unable to save changes');
            }
        },
        error: function () {
            document.triggerNotification('x', 'Unable to process your request, ajax file not found');
            return false;
        }
    });