【问题标题】:Cloning rows with jquery使用 jquery 克隆行
【发布时间】:2017-09-21 06:58:41
【问题描述】:

我有一个表,应该可以选择添加一行和删除一行。我使用 jquery 插入 HTML,它运行良好,但有人告诉我这种方法可能有缺点,我宁愿考虑克隆。在做了更多研究之后,我尝试在我的网站中实现它。它似乎不起作用。

我的html代码:

<div class="row">
                            <div class="col-md-12">
                                <br/>
                                <!--A card inside the second tab, and the table is inside the card-->
                                <div class="card">
                                    <div class="header">
                                        <h4 class="title">1.1 Teaching</h4>
                                        <p class="category">Please Add the Courses taught in the academic year</p>
                                    </div>
                                <div class="content">
                                    <div class="content table-responsive table-full-width" id="t_table">
                                        <table class="table table-hover table-striped" id="t_tab_logic">
                                            <thead>
                                                <th>#</th>
                                                <th>Course</th>
                                                <th>Section</th>
                                                <th>Session</th>
                                                <th>Term</th>
                                                <th>Day/Evening</th>
                                                <th>%age Taught</th>
                                                <th>Load/Overload</th>
                                                <th>Enrolment</th>
                                                <th>Number of T.As/IAs</th>
                                            </thead>
                                            <tbody>
                                                <tr class="tr_clone">
                                                <td>
                                                    1
                                                </td>
                                                <td>
                                                    <input type="text" name='t_name0'  placeholder='Name' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="text" name='t_section0' placeholder='Section' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="text" name='t_session0' placeholder='Session' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="number" name='t_term0' placeholder='Term' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="text" name='t_day0' placeholder='D/E' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="number" name='t_%age0' placeholder='%age' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="text" name='t_load0' placeholder='Load/Over' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="number" name='t_enrolment0' placeholder='#' class="form-control"/>
                                                </td>
                                                <td>
                                                    <input type="number" name='t_number0' placeholder='#' class="form-control"/>
                                                </td>
                                                </tr>

                                            </tbody>
                                        </table>

                                        <div class="row">
                                            <div class="col-sm-2">
                                                <a id="t_add_row" class="btn btn-primary btn-fill pull-left">Add Row</a>
                                            </div>
                                            <div class="col-sm-2 col-sm-offset-8">
                                                <a id="t_delete_row" class="btn btn-submit btn-fill pull-right">Delete Row</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                </div>
                            </div>

                        </div>

我的 java 脚本代码如下:

$(document).ready(function () {

$("#t_add_row").live('click', function(){
   var $tr = $(this).closest('.tr_clone');
   var $clone = $tr.clone();
   $clone.find(":text").val('');
   $tr.after($clone);
});

});

我也想实现删除行链接,我觉得在我实现添加行链接之后会实现。 任何帮助将不胜感激,感谢您的耐心等待,我是 jQuery 的初学者。

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

试试这个代码

$("body").on('click','#t_add_row', function(){
   var tr = $('#t_tab_logic .tr_clone:last');
   var clone = tr.clone();
   clone.find("input").val('');
   tr.after(clone);
});

【讨论】:

  • 太棒了。非常感谢你的帮助!您能否也给我一些关于如何实现删除行部分的想法?
  • $("body").on('click','.t_del_row', function(){ $(this).parents('.tr_clone:eq(0)').remove( ); });
  • 谢谢,我试着做这样的事情: $("body").on('click','#t_delete_row', function(){ $('#t_table_logic .tr_clone:last') 。消除(); });但这似乎不起作用。
猜你喜欢
  • 2012-05-16
  • 2014-02-21
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多