如何使用javascript将此行添加到具有此结构的表中? [英] How can I add rows to a table with this structure using javascript?

查看:82
本文介绍了如何使用javascript将此行添加到具有此结构的表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript将此行添加到具有此结构的表中?



基本上我希望能够单击按钮并在其中创建新行下面使用的每个列的表。例如,它可能是名称:新书,数量:30等



How can I add rows to a table with this structure using javascript?

Basically I want to be able to click a button and have a new row created within the table for each of the columns used below. So for instance, it could be name: new book, quantity:30 etc.

<table id="dynamictable">
              <tbody>
                <tr>
                  <td class="name">My Favorite book</td>
                  <td class="quantity">x 20</td>
                  <td class="unit_price">£8.99</td>
                  <td class="total">£179.80</td>
                  <td class="remove"><img src="image/cross.png"></td>
                </tr>
               </tbody>
            </table>

推荐答案

使用jQuery,您可以轻松添加如下所示的行

Using jQuery you can easily add rows like below


' #buttonId')。点击( function ()
{
string htmlRow = < tr>< td class = name >我最喜欢的book2< / td> ;< / tr>< / tbody>< / table>< table>< tbody>< tr>< td class =数量 > x 20< / td>< / tr>< / tbody>< / table>< table>< tbody>< tr>< ; td class = unit_price >£8.99< / td>< / tr>< ; / tbody>< / table>< table>< tbody>< tr>< td class = >£179.80< / td>< / tr>< / tbody>< / table>< table>< tbody>< tr>< td class = remove >< img src = image / cross.png >< / img>< / td>< / tr>;
('#buttonId').click(function() { string htmlRow="<tr><td class="name">My Favorite book2</td></tr></tbody></table><table><tbody><tr><td class="quantity">x 20</td></tr></tbody></table><table><tbody><tr><td class="unit_price">£8.99</td></tr></tbody></table><table><tbody><tr><td class="total">£179.80</td></tr></tbody></table><table><tbody><tr><td class="remove"><img src="image/cross.png"></img></td></tr>";


' #dynamictable> tbody')。append(htmlRow) );
}
('#dynamictable>tbody').append(htmlRow); }





希望这有帮助



Hope this helps


这篇关于如何使用javascript将此行添加到具有此结构的表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆