代码之家  ›  专栏  ›  技术社区  ›  Joao Pereira

初学者javascript问题

  •  0
  • Joao Pereira  · 技术社区  · 16 年前

    这应该很容易,但由于我是编程新手,特别是javascript,我无法理解。

    假设我有以下javascript代码:

    var InjectClientValue = Class.create();
    
    
    InjectClientValue.prototype = {
        initialize: function( sourceElement, eventElement, updateElement ) {
            this.sourceElement = $(sourceElement);
            this.element = $(eventElement);
            this.updateElement =$(updateElement)
            this.element.observe('click',this.onClick.bindAsEventListener(this));
        },
        onClick: function(event) {
            new Ajax.Request(this.element.href+"/"+this.sourceElement.value, {
                method:'get', 
    
                onSuccess: function(transport) {
    
                    //How do I access the instance variable updateElement in InjectClientValue_
                //updateElement.update(transport.responseJSON.content);
                }
            });
            event.stop();
    
        }
        }
    

    我需要的是从onSuccess of new Ajax.Request访问initialize中设置的变量updateElement。我怎么做?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Tinister    16 年前

    试试这个:

    onClick: function(event) {
        var thisVariable = this;
    
        new Ajax.Request(this.element.href+"/"+this.sourceElement.value, {
            method:'get', 
    
            onSuccess: function(transport) {
                thisVariable.updateElement.update(transport.responseJSON.content);
            }
    
        2
  •  0
  •   Kurt Payne    16 年前

    onClick: function(event) {
        new Ajax.Request(this.element.href+"/"+this.sourceElement.value, {
            method:'get', 
            onSuccess: function(transport) {
                InjectClientValue.updateElement.update(transport.responseJSON.content);
            }