我只想在輸入使用jQuery 1.7.2和Backbone.js更改值時觸發事件。試圖通過傳遞函數來綁定回調會引發錯誤
目前,我有以下(工作)
MyView: Backbone.View.extend({
initialize: function() {
this.colorInput = $("<input />", {
"id": "color",
"name": "color",
"value": this.model.get("color")
});
var self = this;
this.colorInput.on("change", function() {
self.changeColor();
});
},
changeColor: function() {
var color = this.colorInput.val();
this.model.set("color", color);
}
});
我試圖做它的其他方式,我只是通過我的功能。
this.colorInput.on("change", this.changeColor, this);
但是,試圖這樣做時,這種方式,它拋出
((jQuery.event.special [handleObj.origType] || {})。處理|| handleObj錯誤。處理程序).apply不是函數
.apply(matched.elem,args);
這我無法弄清楚。任何想法爲什麼這種方式不起作用?
+1,是的,這裏的意見相同。我也有問題需要一個工作示例,_onlive created_ DOM元素不會觸發'change'事件,我放棄了,因爲它不是與問題有關的問題。 – fguillen
檢查我的編輯,以防萬一我錯了 – fguillen
@fguillen:我半解開了編輯,我想'.on'正在做一個['isPlainObject'](http://api.jquery.com/jQuery.isPlainObject /)檢查其第一個參數以選擇使用哪種形式,第一個參數不是事件映射,因此它與第一種形式的「.on」一起使用,然後我們又試圖調用視圖對象,如功能。你有沒有「改變不會觸發,直到焦點改變」的問題? –