我有以下代码,我无法在OnKeyUp函数中获取对父对象的引用。据我所知,在OnKeyUp方法中,“this”指的是文本框。但是我如何访问父对象,以便var textboxID获得正确的值?
function $MyObject() {
this.Control = {
inputBox: "#inputBox1",
name: "Control1",
BindEvent: function () {
$(this.inputBox).keyup(this.OnKeyUp);
},
OnKeyUp: function () {
var textBoxID = this.inputBox;
alert(textBoxID);
}
}
}
$(document).ready(function () {
var object1 = new $MyObject();
object1.Control.BindEvent();
});
答案 0 :(得分:1)
function $MyObject() {
var self = this.Control = {
inputBox: "#inputBox1",
name: "Control1",
BindEvent: function () {
$(self.inputBox).keyup(self.OnKeyUp);
},
OnKeyUp: function () {
var textBoxID = self.inputBox;
alert(textBoxID);
}
};
}