2014-02-20 33 views
4

我有一個jbuery庫「Tablesorter」的表建設,我想在這個表中添加一行,當我點擊一個按鈕,但是當我添加該行時,這不需要CSS的tablesorter的..所以我不能選擇他或CSS的「奇」,「偶」不工作..用tablesorter在表中添加一行jquery

$("#ajouterCodePiece").click(function(){ 
    $('#tablesorter-demo-gauche tr:last').after('<tr'><td>'+$('#codepiece option:selected').text()+'</td><td>'+$('#mode option:selected').text()+'</td></tr>'); 
}); 

如何爲的tablesorter的新增CSS和jQuery在我的新行做..謝謝

+2

你的'''不匹配的開始! '。'('​​'....' –

+0

像這樣的'$('#codepiece option:selected')'的任何單引號需要通過反斜線轉義或用雙引號替換**「」* * – dreamweiver

+0

好吧,但我的行被添加在表中,但沒有庫的tablesorter的CSS ..例如..如果我添加行==>它被添加到表,但我不能選擇他,而不是css的「奇怪」或「even」等等...... – user1814879

回答

2

Tablesorter將所有表內容存儲在緩存中,因此爲了告訴tablesorter某些內容已經更改,您需要觸發更新:

$('table').trigger('update'); 

這本updated demo

$("#ajouterCodePiece").click(function() { 
    $('#tablesorter-demo-gauche tr:last').after('<tr><td>' + $('#codepiece option:selected').text() + '</td><td>' + $('#mode option:selected').text() + '</td></tr>'); 
    $('#tablesorter-demo-gauche').trigger('update'); 
}); 
+0

非常感謝。行「奇數」「偶數」的工作,但我不能選擇新的行..你可以在這裏看到它...如果你點擊第1或第2行它確定http://jsfiddle.net/t988C/ 35/ – user1814879

+0

將突出顯示功能更改爲綁定到[委託事件](http://api.jquery.com/on/#direct-and-delegated-events),如下所示:'$(「#tablesorter-demo-gauche 「).on(」click「,」tr「,function(){});' - 這是[更新演示](http://jsfiddle.net/Mottie/t988C/40/) – Mottie

+0

。 謝謝 – user1814879