我在输入字段上触发绑定时将函数设置为未定义
I am getting function as undefined while i trigger a bind on input field
我听到输入,当输入被更改时,我正在获取值。 作为初始,我将传递 1 作为默认值。 所以之后用户更改值我应该得到值,
但是我得到错误:undefined is not a function
问题是什么..?
这是我的代码:
var docLoader = function (params) {
window.container = window.container || $("#tenderContent");
return {
init : function () {
this.container = container.find("#documentscroll");
this.inputPage = container.find("#inputpage");
this.width = this.container.width();
this.height = this.container.height();
this.preload();
this.inputChange();
$(this.inputPage).bind("change paste keyup", this.inputChange);
},
preload : function () {
var that = this;
this.container.load("../common/preloader/index.html",
function(msg){
$('#mask').css({width:that.width,height:that.height});
});
},
//load page
loadPage : function (num) {
this.container.load("Doc/chapter"+num+"/index.html");
},
//input change
inputChange : function (e) {
var inputVal = e != undefined ? e.target.value : 1;
this.loadPage(inputVal); //while page load it works, getting value from input, on change i am getting error.
}
}
}
jQuery(document).ready(function($) {
docLoader().init();
$(window).resize(function(){
docLoader().init();
});
});
在这个
函数(inputChange:
)中,this
引用当前($(this.inputPage)
)元素,这就是为什么你会得到错误(因为在元素中没有方法loadPage
)。要修复它,您需要绑定this
(这是对位于return {}
中的object
的引用)才能运行,有几种方法可以做到这一点
$(this.inputPage).bind("change paste keyup", this.inputChange.bind(this));
或
var _this = this;
$(this.inputPage).bind("change paste keyup", function (e) {
_this.inputChange(e)
});
或
$(this.inputPage).bind("change paste keyup", $.proxy(this.inputChange, this));
关于 $.proxy
相关文章:
- javascript定时函数-预期的标识符
- 用于异步函数中的循环和定时问题
- 从函数(咖啡脚本)内以定时间隔触发函数
- 我在输入字段上触发绑定时将函数设置为未定义
- 如何通过单击取消javascript函数启动的定时重定向
- 为什么在使用返回的RequireJS函数中声明的Click绑定时未定义
- web服务器php上的定时函数调用?javascript
- Javascript定时器函数
- Javascript定时器切换函数
- 使用动态参数禁用setInterval函数中的定时器
- 当eval带有绑定时,Nashorn找不到函数
- 为什么clearInterval()函数不停止javascript定时器
- 防止函数在主干中绑定时被调用两次
- 如果IE fork执行定时函数
- “infinite"设置interval函数“定时”
- javascript setTimeout定时器步长函数
- 使用`?`在JavaScript定时器函数返回中
- Javascript定时了我的函数
- setInterval 函数不在定时间隔上运行,只是连续重复,没有时间
- 如何用定时器协调2个函数->javascript