0

我有以下代码:

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) 未定义

你能否让我知道我做错了什么。

4

1 回答 1

8

updateTotalLabourItems调用中的一个函数

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);
   });
});
于 2012-10-18T07:54:14.823 回答