[英]Add data dynamically to a table row
這個問題與我先前在此鏈接中提出的問題有關。 現在,我正在嘗試做相反的事情。 我在這里有一個動態表(可以在其中添加和刪除行),並且應該檢索用戶在表的最新用法(通過按鈕觸發)中輸入的任何數據,並將其張貼在同一表中(如果相同)按鈕)。
這是我用來從動態表中獲取數據的代碼:
$('#tableSearchMainAccount1 tr').each(function(){
var td = '';
if ($(this).find("td:first").length > 0) { // used to skip table header (if there is)
$(this).find('option:selected').each(function(){
td = td + $(this).text()+ ',';
});
td = td + $(this).find('input').val();
filt[ctr]=td;
ctr += 1;
}
});
alert (filt); //alert output like name,equals,ann,age,equals,11
現在,我要做的是,當我再次單擊該按鈕時,以前的輸入數據將再次發布到我的動態表中。
我的桌子看起來像這樣:
<table> <tr><th>field</th><th>comparison</th><th>value</th></tr>
<tr>
<td>
<select style="width:5em;" class="field">
<option>name</option>
<option>age</option>
<option>sex</option>
</select>
</td>
<td>
<select style="width:5em;" class = "comp">
<option>equals</option>
<option>starts with</option>
<option>not equal to</option>
</select>
</td>
<td><input type="text" class = 'value'></td>
<td><img class="add" src="css/images/add.png" /></td> // this is used to add new row
</tr>
</table>
當再次顯示表格時,用戶在表格中輸入的最后數據應顯示在表格中。 如何將數據添加到動態表中?
編輯:
單擊該按鈕后,將打開一個對話框,其中包含我的動態表,並且每當用戶關閉對話框時,該表中的所有行都將與用戶輸入的數據相同。 我已經創建了一個變量,用於保存表中最新輸入的數據。 因此,當再次單擊按鈕時,我希望表生成或顯示用戶的最后輸入數據。 輸出應與最新輸入數據相同。
假設您有一個id為add
的按鈕,並且想在表尾添加一個表行:
$('#add').on('click', function() {
var $lastTr = $('table tr:last');
$lastTr.clone().insertAfter($lastTr);
$('#add', $lastTr).remove();
});
使用.clone
幫助我們克隆最后一行,並將其追加到末尾。
小提琴: http : //jsfiddle.net/garreh/w9fXJ/
只是為了預防您的下一個問題,這是刪除一行的附加內容;-)
$('.remove_row').on('click', function() {
$(this).closest('tr').remove();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.