2012-01-25 41 views
0

我想用jQuery編輯表格中的數據。 的HTML標記是:jQuery:如何更新值到位?

<tr id="unique_rid1"> 
    <td>15/01/2012</td> 
    <td>4</td> 
    <td><a href="#" class="edit_work_done" id="unique_rid1">Edit</a></td> 
</tr> 

當用戶點擊「編輯」,這個環節變得「更新」,表格單元格contaning 4號變成文本字段。 以下是一個jQuery代碼:

//change the work done to be a form when "edit" is clicked 
var toUpdate; 
$(".edit_work_done").click(function(){ 
    $(this).removeClass('edit_work_done').addClass('update_work_done').html("Update!"); 
    toUpdate = $(this).parent().parent().children('td').eq(1); 
    var Hours = toUpdate.html(); 
    toUpdate.html('<input type="number" value="'+ Hours +'" class="new_hour span1"/>'); 
}); 
//change the work done to be static value when "Update!" is clicked 
$(".update_work_done").live("click", function(){ 
    $(this).removeClass('update_work_done').addClass('edit_work_done').html("Edit"); 
    var newHours = $(this).parent().parent().find("input.new_hour").val(); 
    toUpdate = $(this).parent().parent().children('td').eq(1); 
    toUpdate.html(newHours); 
}); 

然而,「活」,立即抓住了階級變化,不允許<input>標籤拿出或鏈接轉換到更新!

問題:我該如何綁定這個事件? (作爲一個簡單的「點擊」事件不會捕捉動態更新的類變化)

+2

'活()'已被棄用,給現場。使用'on()'或'delegate()' – xbonez

回答

1

你應該既

var toUpdate; 
$(".edit_work_done").live("click", function(){ 
    $(this).removeClass('edit_work_done').addClass('update_work_done').html("Update!"); 
    toUpdate = $(this).parent().parent().children('td').eq(1); 
    var Hours = toUpdate.html(); 
    toUpdate.html('<input type="number" value="'+ Hours +'" class="new_hour span1"/>'); 
}); 
+0

sathishkumar:工作!謝謝 :) –