如何使用动态插入的字段

时间:2012-10-20 23:47:58

标签: javascript jquery

我有以下div类'labouritems',每当用户点击一个按钮时,它就会将一个新实例插入到dom中。

我需要能够计算'.rate'和'.hours'字段并设置'.total'字段的答案。

现在这只是用户正在处理的'labouritems'的实例,即:不应该更新另一个实例上的字段总数'所以基本上只是在该行内工作。

下面是我正在使用的html,下面是我所拥有的代码,它仅适用于'labouritems'的第一个实例,但是当我插入一个新代码时,它没有做任何事情。

 <div class="labouritems">
        <label for="quote_labouritems_attributes_0_description">Description</label>
        <input class="input-medium" id="quote_labouritems_attributes_0_description" name="quote[labouritems_attributes][0][description]" size="30" type="text" value="concrete the house">
        <label for="quote_labouritems_attributes_0_hours">Hours</label>
        <input class="input-mini hours" id="quote_labouritems_attributes_0_hours" name="quote[labouritems_attributes][0][hours]" size="30" type="text" value="33">
        <label for="quote_labouritems_attributes_0_total">Total</label>
        <input class="input-mini total" id="quote_labouritems_attributes_0_total" name="quote[labouritems_attributes][0][total]" size="30" type="text" value="1089">
        <label for="quote_labouritems_attributes_0_Mup/Hr">Mup/hr</label>
        <input class="input-mini" id="quote_labouritems_attributes_0_muphourrate" name="quote[labouritems_attributes][0][muphourrate]" size="30" type="text" value="0">
          <label for="quote_labouritems_attributes_0_Mup/Total">Mup/total</label>
        <input class="input-mini" id="quote_labouritems_attributes_0_muptotal" name="quote[labouritems_attributes][0][muptotal]" size="30" type="text" value="0">
        <label for="quote_labouritems_attributes_0_Mup/%">Mup/%</label>
        <input class="input-mini" id="quote_labouritems_attributes_0_muppercent" name="quote[labouritems_attributes][0][muppercent]" size="30" type="text">
        <label for="quote_labouritems_attributes_0_Contractor">Contractor</label>

        <input class="input-small" data-autocomplete="/contractors/autocomplete_contractor_firstname" data-update-elements="{&quot;id&quot;:&quot;#quote_labouritems_attributes_0_contractor_id&quot;,&quot;hourly_rate&quot;:&quot;#quote_labouritems_attributes_0_contractor_rate&quot;}" id="quote_labouritems_attributes_0_contractor_name" name="quote[labouritems_attributes][0][contractor_name]" placeholder="Seach" size="30" type="text" value="bing lee">
        <input id="quote_labouritems_attributes_0_contractor_id" name="quote[labouritems_attributes][0][contractor_id]" type="hidden" value="1">
        <label for="quote_labouritems_attributes_0_Rate/hr">Rate/hr</label>
        <input class="input-mini rate" id="quote_labouritems_attributes_0_contractor_rate" name="quote[labouritems_attributes][0][contractor_rate]" size="30" type="text" value="33">
        <label for="quote_labouritems_attributes_0_Work %">Work %</label>
        <input class="input-mini" id="quote_labouritems_attributes_0_progress_percent" name="quote[labouritems_attributes][0][progress_percent]" size="30" type="text" value="55">
        <label for="quote_labouritems_attributes_0_Work $">Work $</label>
        <input class="input-mini" id="quote_labouritems_attributes_0_progress_payment" name="quote[labouritems_attributes][0][progress_payment]" size="30" type="text">

        <input id="quote_labouritems_attributes_0__destroy" name="quote[labouritems_attributes][0][_destroy]" type="hidden" value="false"><a href="javascript:void(0)" class="btn btn-small btn-danger remove_nested_fields" data-association="labouritems">Delete</a>
    </div>​




 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
$('.hours').on("keyup", function() {
   $('.labouritems').each( function(key,value){
      LabourItems.init(this);
   });
});

1 个答案:

答案 0 :(得分:0)

$(documents).on("keyup", '.hours', function() {
    LabourItems.init(this.closest('.labouritems'));
});

这将 1.在文档上附加hours.keyup事件,而不是每小时元素
2.仅更新已更改元素的总数