jquery - 克隆表的第n行?
每当有人按下添加行按钮时,我尝试使用 jquery 克隆表行。谁能告诉我我的代码有什么问题吗?在我看来,我正在使用 HTML + smarty 模板语言。这是我的模板文件的样子:
<table>
<tr>
<td>Description</td>
<td>Unit</td>
<td>Qty</td>
<td>Total</td>
<td></td>
</tr>
<tbody id="entries">
{foreach from=$arrItem item=i name=inv}
<tr>
<td>
<input type="hidden" name="invoice_item_id[]" value="{$i.invoice_item_id}"/>
<input type="hidden" name="assignment_id[]" value="{$i.assignment_id}" />
<input type="text" name="description[]" value="{$i.description}"/>
</td>
<td><input type="text" class="unit_cost" name="unit_cost[]" value="{$i.unit_cost}"/></td>
<td><input type="text" class="qty" name="qty[]" value="{$i.qty}"/></td>
<td><input type="text" class="cost" name="cost[]" value="{$i.cost}"/></td>
<td><a href="javascript:void(0);" class="delete-invoice-item">delete</a></td>
</tr>
{/foreach}
</tbody>
<tfoot>
<tr><td colspan="5"><input type="button" id="add-row" value="add row" /></td></tr>
</tfoot>
</table>
这是我的 Jquery Javascript 调用,我知道当我放入alert() 语句时该调用会被触发。所以问题是我不知道 jquery 是如何工作的。
$('#add-row').live('click', function() {$('#entries tr:nth-child(0)').clone().appendTo('#entries');});
那么我做错了什么?
I'm trying to use jquery to clone a table row everytime someone presses the add-row button. Can anyone tell me what's wrong with my code? I'm using HTML + smarty templating language in my view. Here's what my template file looks like:
<table>
<tr>
<td>Description</td>
<td>Unit</td>
<td>Qty</td>
<td>Total</td>
<td></td>
</tr>
<tbody id="entries">
{foreach from=$arrItem item=i name=inv}
<tr>
<td>
<input type="hidden" name="invoice_item_id[]" value="{$i.invoice_item_id}"/>
<input type="hidden" name="assignment_id[]" value="{$i.assignment_id}" />
<input type="text" name="description[]" value="{$i.description}"/>
</td>
<td><input type="text" class="unit_cost" name="unit_cost[]" value="{$i.unit_cost}"/></td>
<td><input type="text" class="qty" name="qty[]" value="{$i.qty}"/></td>
<td><input type="text" class="cost" name="cost[]" value="{$i.cost}"/></td>
<td><a href="javascript:void(0);" class="delete-invoice-item">delete</a></td>
</tr>
{/foreach}
</tbody>
<tfoot>
<tr><td colspan="5"><input type="button" id="add-row" value="add row" /></td></tr>
</tfoot>
</table>
Here's my Jquery Javascript call, which I know gets fired when I put in an alert() statement. So the problem is with me not knowing how jquery works.
$('#add-row').live('click', function() {$('#entries tr:nth-child(0)').clone().appendTo('#entries');});
So what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,不存在
nth-child(0)
这样的东西,nth-child 以 1 开头First off there is no such thing as
nth-child(0)
, nth-child starts with 1尝试使用:
看看是否有帮助......
Try using:
See if that helps....
啊我明白了。如果我想选择第一行,则 nth-child(0) 应该是 nth-child(1) 。从1开始计数
ah i figured it out. nth-child(0) should be nth-child(1) if i want to select the first row. Counting starts from 1