我正在尝试将变量设置为选择菜单处理程序内选择菜单的选定值。问题是在选择处理程序的上下文中无法识别我的变量,如果我将选择处理程序传递给变量上下文,我将无法再访问所选变量的值。有人知道解决方法吗?
function object(){
this.testVar;
this.handler;
}
function makeObject(){
var result = new object();
result.testVar;
result.handler = handler;
return result;
}
function handler(){
alert($(this).val());
alert(this.testVar);
//ultimately this is what I want to do
this.testVar = $(this).val();
}
//If I do it this way $(this).val() is defined but not testVar
$("#test-select").on("change", {
testdata: this.testVar
}, this.handler);
//If I do it this way testVar is defined but not $(this).val()
$("#test-select").on("change", {
testdata: this.testVar
}, $.proxy(this.handler,this));