我正在將一個表格行插入到表格中,無論如何它會變得混亂。我有一個函數會在3個文本字段中的一個發生變化時被調用,這很好,我使用「inputfield」類來附加事件。但是,當我插入行時,即使它具有相同的類標記,也不會發生相同的行爲。jquery綁定事件,插入的表格行將不會響應事件
所以我試圖將事件綁定到它,這不工作任何想法爲什麼?
//code for inserted row
var $html='<tr class="newrow" id="newrow'+$totrecords+'"><td><div id="discountlist_'+$totrecords+'">'+$totrecords+'</div></td><td><label for="ProductProductCode'+$totrecords+'"></label><input name="data[Product][product_code'+$totrecords+']" type="text" id="ProductProductCode'+$totrecords+'"/></td><td><label for="ProductHeight'+$totrecords+'"></label><input name="data[Product][height'+$totrecords+']" type="text" value="0" id="ProductHeight'+$totrecords+'"/></td><td><label for="ProductWidth'+$totrecords+'"></label><input name="data[Product][width'+$totrecords+']" type="text" value="0" id="ProductWidth'+$totrecords+'"/></td><td><label for="ProductPrice'+$totrecords+'"></label><input name="data[Product][price'+$totrecords+']" type="text" id="ProductPrice'+$totrecords+'" class="fieldinput" /></td><td><label for="ProductDiscountPercent'+$totrecords+'"></label><input name="data[Product][discount_percent'+$totrecords+']" type="text" id="ProductDiscountPercent'+$totrecords+'" class="fieldinput"/></td><td><label for="ProductDiscountListprice'+$totrecords+'"></label><input name="data[Product][discount_listprice'+$totrecords+']" type="text" id="ProductDiscountListprice'+$totrecords+'" /></td><td><label for="ProductQuantity'+$totrecords+'"></label><input name="data[Product][quantity'+$totrecords+']" type="text" id="ProductQuantity'+$totrecords+'" class="fieldinput"/></td><td><div class="total" id="total_'+$totrecords+'"></div</td>';
//method 1 insert the row
$(this).closest('TR').after($html);
//bind event to productprice table
$("#ProductPrice"+$totrecords).bind("change",calculateTotal);
function I'm trying to call
//custom function
var calculateTotal = function(){
alert('ok');
var $discountpercent = null;
var $total=null;
var $quantity=null;
var $id=null;
//get id of textbox
var $id=$(this).attr('id');
//get the row id
//need to fix this and get all chars to the right of the
$id=$id.toString();
var myArray = $id.split('_');
$id=myArray[1];
var $listprice= $("#listprice_"+$id).val();
//turn entered number into %
$discountpercent= $("#discountpercent_"+$id).val()/100;
$discountlistprice=$listprice-($listprice*$discountpercent);
$quantity=$("#quantity_"+$id).val();
//calculate total
$total=$quantity*$discountlistprice;
//set the value
$("#discountlistprice_"+$id).val($discountlistprice);
//set the total by changing the total div
$("#total_"+$id).html($total);
}
請創建一個小提琴用盡可能少的代碼儘可能地幫助你的問題更清楚。 – iambriansreed 2012-03-16 19:54:25