简体   繁体   English

了解更改事件中jQuery中'this'的范围

[英]Understanding Scope of 'this' in jQuery on change event

I wrote a quick custom extension for jQuery for a project I am working on. 我为我正在研究的项目编写了一个jQuery的快速自定义扩展。 I am having a hard time understanding the scope of 'this' in a custom onChange method I would like implement. 我很难理解我希望实现的自定义onChange方法中'this'的范围。 If left out the middle of my code where I am calling the webservice but if you checkout the last two methods, you will see where my problem is. 如果省略我的代码中间我调用webservice,但如果你检查最后两个方法,你会看到我的问题所在。 I want to call the updateDetails method with the selected value changes. 我想用所选的值更改调用updateDetails方法。 However, when that method is called within the onchange event, I obviously lose the scope of "this" as this.materialListResponse comes back as undefined in this context. 但是,当在onchange事件中调用该方法时,我显然会失去“this”的范围,因为this.materialListResponse在此上下文中返回为undefined。 Any help on helping me understand this would be greatly appreciated. 任何帮助我理解这一点的帮助将不胜感激。

$.fn.appendMaterials = function (options) {

       this.options = options;

       //Set Default Options

       this.defaults = {

           typeID: '66E1320D-51F9-4900-BE84-6D5B571F9B80'

       };

       this.options = $.extend({}, this.defaults, options);

       //
       Code here to call web service and generate response XML
       //

       this.materialListResponse = $.xml2json(

       $.parseXML($(this.materialListWebservice()).find("GetMaterialTreeResponse").text())).Materials.Material;

       this.appendOptionString = function () {
           var i = 0;
           this.optionString = '<option>'
           for (i = 0; i < this.materialListResponse.length; i++) {
               this.optionString += '<option>' + this.materialListResponse[i].MaterialCode + '</option>';
           };

           this.append(this.optionString);

           return this;
       };

       this.appendOptionString();

       this.updateDetails = function () {
           for (i = 0; i < this.materialListResponse.length; i++) {

               if (this.materialListResponse[i].MaterialCode === this.val()) {

                   $('#table1 #MaterialDescription').val(this.materialListResponse[i].Description);

               }
           }
       }

       this.change(this.updateDetails)

   };    

pass the object this as data to the event: 传递物体this作为数据发送到事件:

this.change({that: this}, this.updateDetails)

and then you can access that in the scope of the event callback 然后您可以在事件回调范围内访问that

this.updateDetails = function(event) {
    var that = event.data.that;
    ...
}

RESOURCES 资源

The event handler will be called later, when you have exited the extension. 退出扩展后,将在稍后调用事件处理程序。 It's called in the scope of the element, so this will be the element that has changed. 它在元素的范围内调用,因此this将是已更改的元素。

Copy the reference to the list to a local variable, and use that in the event handler: 将对列表的引用复制到局部变量,并在事件处理程序中使用它:

var list = this.materialListResponse;
this.updateDetails = function() {
    for (i = 0; i < list.length; i++) {
        if (list[i].MaterialCode === this.val()) {
            $('#table1 #MaterialDescription').val(list[i].Description);
        }
    }
}

By using the local variable in the function, the variable will be part of the closure for the function, so it will survive the scope of the extension method where it is declared. 通过在函数中使用局部变量,变量将成为函数闭包的一部分,因此它将在声明它的扩展方法的范围内存活。

When a method is called in JavaScript as a callback it behaves as a function. 当一个方法在JavaScript中作为回调调用时,它表现为一个函数。 In this case "this" refers to the owner of this function, usually the Window object in a Web browser. 在这种情况下,“this”指的是此函数的所有者,通常是Web浏览器中的Window对象。

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

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