2016-06-21 31 views
0
<div id="parent_${id}"></div> 

var tableHead = "<table id='ftTableID'><thead><th class='th4'>First Name</th><th class='th5'>Last Name</th><th>Action</th></thead>")}"+""; 

$('#parent_itemId').prepend(tableHead); 
$('#ftTableID').append("</table>"); 

在此之後追加新trdiv我們是否可以得到布爾值(true或false)是否刪除的tr是否在div內?

var $newElem = $("<tr>").html(elem).attr({'id': id + num1, 'no': num1}); 

// creates the "Remove" button 
var $removeButton = $('<input type="button" class="deleteButton" value="Delete" /></tr>').appendTo($newElem); 

$removeButton.attr({ 
    id: 'remove_' + id, 
    value: removeBtnLabel ? removeBtnLabel : 'Remove', 
    disabled: 'disabled' 
}); 

// binds handler to the 'click' JS event of the "Remove" button 
$removeButton.click(function() { 
    removeItem(id, num1, min); 
}); 

追加trdiv

$('#parent_' + id).append($newElem); 

首先tr將在上述div id被追加與ADD取消旁邊的按鈕 。雖然我們點擊ADD按鈕,下一步tr將加入ADD取消旁邊的按鈕。 ADD按鈕將被省略,從第一個tr只剩下取消按鈕在第一個tr等等。現在當我按CANCELL按鈕時,我需要知道刪除的行(tr)是否是最後一行?

現在,當調用removeItem()函數時,我想查找tr是否是最後的tr

+0

'$( 'TR:去年')'試試這個選擇器':last'或方法'.last()' – guradio

+0

每個'tr'是否都有自己的取消按鈕,它會刪除那個'tr'? – wmash

+0

是的,每個tr都有自己的取消按鈕,可以刪除相應的tr @wmash –

回答

0

下面是HTML動態添加兩行後:

<div id="parent_${id}"> 
    <table> 
      <tr> 
       <td> 
        <input type="button" value="Cancel" id="CancelBtn1" /> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="button" value="Add2" /> 
       </td> 
       <td> 
        <input type="button" value="Cancel" id="CancelBtn2"/> 
       </td> 
      </tr> 
    </table> 
</div> 

您可以使用下面的代碼上的取消按鈕,點擊:

if($(this).closest("tr").next().is("tr")) 
{ 

//it is not the last row 
} 
else 
{ 
//it is the last row 
} 
+0

我附加tr在div中不在表格中..不工作..反正你的時間。 –

+0

在沒有表格的情況下添加tr不是正確的HTML語法。有相同的理由嗎? – AshishJindal

+0

我正在嘗試創建一個動態水平表單。在這種情況下,如果我添加一個表,那麼表頭也將被添加到下一個tr中。所以我已經把桌子的頭部留下了空白,並且附在了div上。我分別附加了tr。 –