jquery - 克隆表的第n行?

发布于 2024-08-31 05:16:38 字数 1845 浏览 2 评论 0原文

每当有人按下添加行按钮时,我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-09-07 05:16:38

首先,不存在 nth-child(0) 这样的东西,nth-child 以 1 开头

First off there is no such thing as nth-child(0), nth-child starts with 1

云归处 2024-09-07 05:16:38

尝试使用:

$("tr:nth-child(0)", "#entries")

看看是否有帮助......

Try using:

$("tr:nth-child(0)", "#entries")

See if that helps....

会发光的星星闪亮亮i 2024-09-07 05:16:38

啊我明白了。如果我想选择第一行,则 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文