值会在所有字段实例上更新

时间:2012-10-19 13:11:31

标签: javascript jquery

我有以下代码,用于计算插入的每个实例(行)的两个字段。

目前它填充了所有“总计”字段,而不仅仅是相应行的字段。

还会在所有总字段中插入“NaN”。

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 = Number(rate) * Number(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);
   });
});

1 个答案:

答案 0 :(得分:0)

您尚未获取字段的

var rate = parseInt($(object).children('.rate').val(), 10);

[使用.first()阅读时不需要.val()隐含

此外,您还应使用.val() 设置总值,而不是.attr('value', ...)