我得到了下面的代码,我对它有问题。除了第一个实例之外,我无法计算“劳动项目”的任何实例的“总”数量,之后的每个实例我都无法计算它。
var LabourItems = {
rate: null,
hours: null,
total: null,
init: function(object) {
this.rate = parseInt($(object).children('.rate').first().val(), 10);
// var rate = $(object).children('.rate').first();
this.hours =parseInt($(object).children('.hours').first().val(), 10);
this.total = this.rate * this.hours;
this.updateTotal(object);
},
updateTotal: function(object) {
$(object).children('.total').first().val(this.total || 0);
}
}
//reactTochange for those inputs that you want to observe
$('.labouritems input').on("keyup", function() {
$('.labouritems').each( function(key,value){
LabourItems.init(this);
});
});发布于 2012-10-21 06:06:26
这很简单:你不需要创建任何“Labouritems”的实例,你只有一个Labouritems对象。
发布于 2012-10-21 06:06:38
您的代码中没有任何“实例”,因为您从未调用过new。
要允许将LabourItems视为对象,请这样声明它:
function LabourItems(object) {
return {
rate: null,
...
}
});然后在事件处理程序中使用new LabourItems(this)。
或者(更有效地,因为每个实例将共享方法的副本,而不是包含它们自己的副本)使用普通的原型声明:
function LabourItems(object) {
this.rate = null,
...
};
LabourItems.prototype.init = function() {
...
};
LabourItems.prototype.updateTotal = function() {
...
};并使用如上的new LabourItems(this)。
https://stackoverflow.com/questions/12993094
复制相似问题