从一个 table 克隆到另一个
Clone from one table to antoher
我刚刚在另一个 post 中看到了解决方案,但当我尝试在我的代码中使用它时它不起作用。
我正在尝试通过按钮将一个 table 上的项目添加到另一个。感谢任何帮助。
html
<div id="container">
<input type="text" name="search" id="search" placeholder="Ingrese el nombre del examen..." size="50">
</input>
<table id="proctable" >
<tr>
<th>Examen</th>
<th>Precio</th>
<th></th>
</tr>
<tr>
<td>hola</td>
<td>10</td>
<td><input class="addBtn" type="button" value="Agregar"></td>
</tr>
</table>
<hr>
<div>
<table id="comptable">
<tr class="header">
<th>Examen</th>
<th>Precio</th>
</tr>
</table>
</div>
</div>
JS
<script type="text/javascript">
var items = [];
$(".addBtn").on("click", function() {
var newTr = $(this).closest("tr").clone();
items.push(newTr);
newTr.appendTo( $("#comptable") );
});
</script>
您的 javascript 实际上按预期工作。由于两件小事,jsFiddle 被破坏了:
您的 javascript 周围有脚本打开和关闭标签。 jsFiddle 实际上显示了一个黄色的音符 "Input plain JavaScript code, no HTML."
输入标签需要自闭,像这样:<input class="addBtn" type="button" value="Agregar"/>
,而不是<input class="addBtn" type="button" value="Agregar"></input>
我刚刚在另一个 post 中看到了解决方案,但当我尝试在我的代码中使用它时它不起作用。
我正在尝试通过按钮将一个 table 上的项目添加到另一个。感谢任何帮助。
html
<div id="container">
<input type="text" name="search" id="search" placeholder="Ingrese el nombre del examen..." size="50">
</input>
<table id="proctable" >
<tr>
<th>Examen</th>
<th>Precio</th>
<th></th>
</tr>
<tr>
<td>hola</td>
<td>10</td>
<td><input class="addBtn" type="button" value="Agregar"></td>
</tr>
</table>
<hr>
<div>
<table id="comptable">
<tr class="header">
<th>Examen</th>
<th>Precio</th>
</tr>
</table>
</div>
</div>
JS
<script type="text/javascript">
var items = [];
$(".addBtn").on("click", function() {
var newTr = $(this).closest("tr").clone();
items.push(newTr);
newTr.appendTo( $("#comptable") );
});
</script>
您的 javascript 实际上按预期工作。由于两件小事,jsFiddle 被破坏了:
您的 javascript 周围有脚本打开和关闭标签。 jsFiddle 实际上显示了一个黄色的音符 "Input plain JavaScript code, no HTML."
输入标签需要自闭,像这样:
<input class="addBtn" type="button" value="Agregar"/>
,而不是<input class="addBtn" type="button" value="Agregar"></input>