我有以下代码:
var LabourItems = {
rate: null,
hours: null,
total: null,
init: function(object) {
var rate = $(object).children('.rate').first();
var hours =$(object).children('.hours').first();
total = rate * hours;
updateTotal(object,total);
},
updateTotal: function(object, total) {
$(object).children('.total').first().attr('value', total)
}
}
//reactTochange for those inputs that you want to observe
$('.hours').live("click", function() {
jQuery.each($('.labouritems'), function(key,value){
LabourItems.init(value);
});
});
并确定未定义的函数错误:
你能告诉我我做错了吗?updateTotal(object,total)未定义
答案 0 :(得分:8)
updateTotal
是LabourItems
中的函数,因此请调用
LabourItems.updateTotal(object,total);
或(仅当从LabourItems
或其实例的任何其他函数调用时)
this.updateTotal(object,total);
更新:
var LabourItems = {
rate: null,
hours: null,
total: null,
init: function(object) {
var rate = $(object).children('.rate').first();
var hours =$(object).children('.hours').first();
this.total = rate * hours;
this.updateTotal(object);
},
updateTotal: function(object) {
$(object).children('.total').first().attr('value', this.total)
}
}
//reactTochange for those inputs that you want to observe
$('.hours').live("click", function() {
jQuery.each($('.labouritems'), function(key,value){
LabourItems.init(value);
});
});