[英]Jquery - Get previous value row in table
我有動態簡單的表,如:
當我點擊編輯按鈕時,我嘗試獲取前一個值單元格。
示例:當我單擊第一個將發出警報的編輯按鈕('a1')
當我點擊第二個編輯按鈕時會發出警報('a2')
我嘗試着
$('.edit').click(function(){
alert($(this).parents('tr').prev().children().eq(1).text());
});
它與第一個編輯按鈕配合良好,因為上一行有一行。
而且它不能使用第二個編輯按鈕。
我怎么能這樣做(通過動態前一行) http://jsfiddle.net/bWjbj/
ps:我正在與下一行合作
alert($(this).parents('tr').nextAll(':eq(' + ($(this).parent().siblings().eq(0).attr("rowspan")-1) + ')').children().eq(1).text());
http://jsfiddle.net/mblase75/XGdkD/
問題是,對於第二個“編輯”按鈕,前一個表行不是您想要的行 - 您希望在此之前有兩行,因為這是行窗口開始的位置。
或者,通用:您希望表行屬於上一個“編輯”按鈕。 但是,在第一個編輯按鈕的情況下,您只需要前一行。
所以,在代碼中:
$('.edit').click(function () {
var idx = $('.edit').index(this); // which Edit button is this?
if (idx > 0) { // first button
var $tr = $('.edit').eq(idx-1).closest('tr'); // previous row
} else { // not first button
var $tr = $(this).closest('tr').prev('tr'); // previous Edit button's row
}
var $td = $tr.find('td:nth-child(2)'); // second <td> of the row
alert($td.text());
});
緊湊版的相同代碼:
$('.edit').click(function () {
var idx = $('.edit').index(this),
$tr = (idx) ? $('.edit').eq(idx-1).closest('tr') : $(this).closest('tr').prev('tr'),
$td = $tr.find('td:nth-child(2)');
alert($td.text());
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.