[英]Cloning table row and adding it to end of table
I am trying to clone and append a table row when the user selects my add rows button. 当用户选择我的添加行按钮时,我试图克隆并追加表行。 I have an empty hidden row that is used to clone.
我有一个空的隐藏行用于克隆。 I can't seem to get it to work how I need it too.
我似乎也无法使其按我的需要工作。
I output my form with PHP and looks something like this: 我用PHP输出表单,看起来像这样:
$budgetRowCount = 0;
echo"<table id='selected_budget_table'>
<tr>
<th>Roofs</th>
<th>Roof Area</th>
<th>Recommendations</th>
<th>Amount</th>
<th>Remove</th>
</tr>";
echo "<tr id='new_budget_row0' style='display: none;'>
<td><input id='budget-roofs' name='budget-roofs[]' /></td>
<td><input id='budget-area' name='budget-area[]' /></td>
<td><input id='budget-recommendation' name='budget-recommendations[]' /></td>
<td><input id='budget-amount' name='budget-amount[]'/> </td>
</tr>";
while ($budgetInfoRow = mysqli_fetch_array($budgetResult)) {
if($budgetRowCount == 0){
echo "<tr id='selected_budget_row". $budgetRowCount ."'>";
echo "<td><input type='text' id='budget-roofs' name='budget-roofs[]' value='".$budgetInfoRow['budget_roofs']."'</td>";
echo "<td><input type='text' id='budget-roof-area' name='budget-roof-area[]' value='".$budgetInfoRow['budget_roof_area']."'</td>";
echo "<td><input type='text' id='budget-recommendation' name='budget-recommendation[]' value='".$budgetInfoRow['budget_recommendation']."'</td>";
echo "<td><input type='text' id='budget-amount' name='budget-amount[]' value='".$budgetInfoRow['budget_amount']."'</td>";
echo "</tr>";
$budgetRowCount++;
}
else{
echo "<tr id='selected_budget_row". $budgetRowCount ."'>";
echo "<td><input type='text' id='budget-roofs' name='budget-roofs[]' value='".$budgetInfoRow['budget_roofs']."'</td>";
echo "<td><input type='text' id='budget-roof-area' name='budget-roof-area[]' value='".$budgetInfoRow['budget_roof_area']."'</td>";
echo "<td><input type='text' id='budget-recommendation' name='budget-recommendation[]' value='".$budgetInfoRow['budget_recommendation']."'</td>";
echo "<td><input type='text' id='budget-amount' name='budget-amount[]' value='".$budgetInfoRow['budget_amount']."'</td>";
echo "<td><a href='#' class='removeRow' data-remove-row='budget_row". $budgetRowCount . "'>Remove</a></td>";
echo "</tr>";
$budgetRowCount++;
}
}
echo "</table>";
echo"<input type='button' value='+' id='addNewBudgetRow' class='addNewBudgetRow'/>";
And this is how I am attempting to clone my row and add it to my table: 这就是我试图克隆行并将其添加到表中的方式:
$(function() {
var $removeIDVal = 0;
$(document.body).on('click', '.addNewBudgetRow', function () {
var $emptyBudgetTableRow = $("#new_budget_row0").clone();
$removeIDVal++
var $emptyBudgetTableRowClone = $emptyBudgetTableRow.clone();
var $newRowID = 'added_budget_row' + $removeIDVal;
$emptyBudgetTableRowClone.attr('id', $newRowID)
$emptyBudgetTableRowClone.children('td').last().after('<td><a href="#" class="removeRow" data-remove-row="' + $newRowID + '">Remove</a></td>');
$(this).closest("fieldset").find("tbody").append($emptyBudgetTableRowClone);
$emptyBudgetTableRowClone.show();
});
});
I had an alert to check if the button was actually firing and my alert showed up no problem, however I can't seem to get it to clone and append properly and I have done this several times elsewhere with no issues. 我有一个警报来检查按钮是否真正触发,并且警报没有出现问题,但是我似乎无法正确克隆和附加它,并且在其他地方我做了几次也没有问题。 Where am I going wrong here?
我在哪里错了?
How can I fix this so that my row gets cloned properly and added to the end of my table? 如何解决此问题,以便正确克隆我的行并将其添加到表的末尾?
You are not selecting your table, since you have no <fieldset>
or <tbody>
. 您没有选择表,因为您没有
<fieldset>
或<tbody>
。 Select it by id. 通过ID选择它。
Alos you were cloning new row twice and you have multiple same ids. 您还克隆了两次新行,并且您有多个相同的ID。
$(document.body).on('click', '.addNewBudgetRow', function () {
var $emptyBudgetTableRowClone = $("#new_budget_row0").clone();
$removeIDVal++
var $newRowID = 'added_budget_row' + $removeIDVal;
$emptyBudgetTableRowClone.attr('id', $newRowID)
$emptyBudgetTableRowClone.children('td').last().after('<td><a href="#" class="removeRow" data-remove-row="' + $newRowID + '">Remove</a></td>');
$('#selected_budget_table').append($emptyBudgetTableRowClone);
$emptyBudgetTableRowClone.show();
});
});
$(function() { var $removeIDVal = 0; $(document.body).on('click', '.addNewBudgetRow', function () { var $emptyBudgetTableRowClone = $("#new_budget_row0").clone(); $removeIDVal++ var $newRowID = 'added_budget_row' + $removeIDVal; $emptyBudgetTableRowClone.attr('id', $newRowID) $emptyBudgetTableRowClone.children('td').last().after('<td><a href="#" class="removeRow" data-remove-row="' + $newRowID + '">Remove</a></td>'); // Select you table by id $('#selected_budget_table').append($emptyBudgetTableRowClone); $emptyBudgetTableRowClone.show(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id='selected_budget_table'> <tr> <th>Roofs</th> <th>Roof Area</th> <th>Recommendations</th> <th>Amount</th> <th>Remove</th> </tr> <tr id='new_budget_row0' style='display: none;'> <td><input id='budget-roofs' name='budget-roofs[]' /></td> <td><input id='budget-area' name='budget-area[]' /></td> <td><input id='budget-recommendation' name='budget-recommendations[]' /></td> <td><input id='budget-amount' name='budget-amount[]'/> </td> </tr> </table> <input type='button' value='+' id='addNewBudgetRow' class='addNewBudgetRow'/>
I've put it up on jsFiddle and found + fixed some issues: 我把它放在jsFiddle上,发现并修复了一些问题:
http://jsfiddle.net/lumpie/nprsdb2m/ http://jsfiddle.net/lumpie/nprsdb2m/
$(function() {
var $removeIDVal = 0;
$(document.body).on('click', '.addNewBudgetRow', function () {
var $emptyBudgetTableRow = $("#new_budget_row0");
$removeIDVal++
var $emptyBudgetTableRowClone = $emptyBudgetTableRow.clone();
var $newRowID = 'added_budget_row' + $removeIDVal;
$emptyBudgetTableRowClone.attr('id', $newRowID)
$emptyBudgetTableRowClone.append('<td><a href="#" class="removeRow" data-remove-row="' + $newRowID + '">Remove</a></td>');
$("#selected_budget_table").append($emptyBudgetTableRowClone);
// Logic to remove a row:
$emptyBudgetTableRowClone.find(".removeRow").click(function() {
$(this).parents("tr").remove();
});
$emptyBudgetTableRowClone.show();
});
});
Ah, just a minute too late, Rene Korss was slightly ahead of me. 啊,迟到了一分钟,Rene Korss稍微领先于我。
But hey: I've included a little extra: logic to make the Remove button work :) 但是,嘿:我加入了一些额外的功能:使“删除”按钮起作用的逻辑:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.