简体   繁体   English

函数中的Javascript访问对象变量

[英]Javascript access object variables from functions

function init_exam_chooser(id,mode)
{
    this.id=id;
    this.table=$("#" + this.id);

    this.htmlRowOrder="<tr>" + $("#" + this.id + " tbody tr:eq(0)").html() + "</tr>";
    this.htmlRowNew="<tr>" + $("#" + this.id + " tbody tr:eq(1)").html() + "</tr>";
    $("#" + this.id + " tbody tr").remove();

    //Arxikopoiisi
    var rowNew=$(this.htmlRowNew);

    rowNew.find("input[type='text']").eq(0).autocomplete({
        source: function (req,resp)
        {

            $.ajax({
                url: "/medilab/prototypes/exams/searchQuick",
                cache: false,
                dataType: "json",
                data:{codeName:req.term},
                success: function(data) {
                    resp(data);
                }
            });

        },
        focus: function(event,ui)
        {
            return false;
        },
        minLength :2

    });

    rowNew.find("input[type='text']").eq(1).autocomplete({
        source: function (req,resp)
        {

            $.ajax({
                url: "/medilab/prototypes/exams/searchQuick",
                cache: false,
                dataType: "json",
                data:{name:req.term},
                success: function(data) {
                    resp(data);
                }
            });

        },
        focus: function(event,ui)
        {
            return false;
        },

        minLength :2
    });
    rowNew.find("input[type='text']").bind( "autocompleteselect", function(event, ui) {
        alert(htmlRowOrder);
        var row=$(htmlRowOrder);

        $(table).find("tbody tr:last").before(row);
        alert(ui.item.id);
});
    rowNew.appendTo($(this.table).find("tbody"));


//this.htmlRowNew

}

The problem is at ,how i can access htmlRowOrder? 问题是,我如何访问htmlRowOrder? I tried this.htmlRowOrder and didnt work.... Any ideas?? 我尝试了this.htmlRowOrder,但没有成功。

rowNew.find("input[type='text']").bind( "autocompleteselect", function(event, ui) {
            alert(htmlRowOrder);
            var row=$(htmlRowOrder);

            $(table).find("tbody tr:last").before(row);
            alert(ui.item.id);
    });

Your issue is that this is not what you think it is inside your event handlers. 您的问题在于, this不是您认为的事件处理程序内部的内容。 Most jQuery event handlers run in the context of the element on which the event was triggered; 大多数jQuery事件处理程序都在触发事件的元素的上下文中运行; what that means is that inside the event handler, this is the element on which the event was triggered. 这意味着在事件处理程序内部, this是触发事件的元素。

You can solve this problem either by waiting for the next revision of JavaScript, which will have Function.prototype.bind baked in, or by setting a reference to your scope object outside the event handler and referring to it inside, similarly to patrick's answer. 您可以通过等待JavaScript的下一个修订版本(包含Function.prototype.bind)来解决此问题,或者通过设置对事件处理程序外部的scope对象的引用并在内部对其进行引用来解决此问题,这与patrick的回答类似。

function(){
    var instance = this;
    this.foo = "abc123";
    $('someselector').click(function(ev){
        this.foo; //causes an error; foo is now the element matching 'someselector'
        instance.foo; //this returns "abc123"
    }
}

Here's a reasonably thorough explanation of the this keyword: 以下是this关键字的合理详尽的解释:

http://www.quirksmode.org/js/this.html http://www.quirksmode.org/js/this.html

You could define the variable outside the function. 您可以在函数外部定义变量。

var rowNew;

function init_exam_chooser(id,mode) {
    ...
    rowNew=$(this.htmlRowNew);
    ...
}

rowNew...

Of course, you will have to call the function before rowNew will reference the value you want to work with. 当然,您必须在rowNew引用要使用的值之前调用该函数。


I used a different variable, but the principle is the same. 我使用了一个不同的变量,但是原理是相同的。

Or, you could have your function return the value you want. 或者,您可以让函数返回所需的值。

function init_exam_chooser(id,mode) {
    // Do stuff
    return myVariable;
}

var anotherVariable = init_exam_chooser(id,mode);

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

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