Javascript从函数访问对象变量(Javascript access object variables from functions)

编程入门 行业动态 更新时间:2024-10-28 07:20:15
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 }

问题在于,我如何访问htmlRowOrder? 我试过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); }); 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? I tried this.htmlRowOrder and didnt work.... Any ideas??

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); });

最满意答案

你的问题是, this不是你认为它在你的事件处理程序中。 大多数jQuery事件处理程序在触发事件的元素的上下文中运行; 这意味着在事件处理程序中, this是触发事件的元素。

您可以通过等待JavaScript的下一个修订版本来解决这个问题,该版本将包含Function.prototype.bind,或者通过在事件处理函数之外设置对象范围对象的引用并引用它,类似于帕特里克的答案。

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" } }

Your issue is that this is not what you think it is inside your event handlers. Most jQuery event handlers run in the context of the element on which the event was triggered; what that means is that inside the event handler, this is the element on which the event was triggered.

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.

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" } }

更多推荐

本文发布于:2023-07-29 18:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   函数   对象   Javascript   functions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!