未定义的javascript函数

时间:2012-10-18 07:53:02

标签: javascript jquery

我有以下代码:

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

你能告诉我我做错了吗?

1 个答案:

答案 0 :(得分: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);
   });
});