jQuery動態(tài)增減行的實例代碼解析(推薦)
先給大家展示下效果圖:
這是沒有增加時的界面:
增加后的界面:
刪除后的界面:
原因分析:
不擅長jquery和JavaScript
場景:
代碼如下:
<table class="table table-bordered"> <thead> <tr> <th style="width: 10px">輪次</th> <th style="width: 100%">比賽時間</th> <th>比賽場地</th> <th>主隊</th> <th>主隊得分</th> <th>客隊</th> <th>客隊得分</th> <th>比賽結(jié)果</th> <th>刪除</th> </tr> </thead> <tbody id="Games_tbody"> <tr> <td> <input type="number" style="width: 40px"/> </td> <td> <input type="date" style="width: 140px"/> </td> <td> <input type="text" style="width: 140px"/> </td> <td> <input type="text" style="width: 140px"/> </td> <td> <input type="number" style="width: 80px"/> </td> <td> <input type="text" style="width: 140px"/> </td> <td> <input type="number" style="width: 80px"/> </td> <td> <input type="text" style="width: 40px"/> </td> <td> <button type="button" class="btn btn-danger btn-xs" id="delete" onclick="$(this).parent('td').parent('tr').remove()">刪除</button> </td> </tr> <tr> <td> <input type="number" style="width: 40px"/> </td> <td> <input type="date" style="width: 140px"/> </td> <td> <input type="text" style="width: 140px"/> </td> <td> <input type="text" style="width: 140px"/> </td> <td> <input type="number" style="width: 80px"/> </td> <td> <input type="text" style="width: 140px"/> </td> <td> <input type="number" style="width: 80px"/> </td> <td> <input type="text" style="width: 40px"/> </td> <td> <button type="button" class="btn btn-danger btn-xs" id="delete" onclick="$(this).parent('td').parent('tr').remove()">刪除</button> </td> </tr> </tbody> </table> <button type="button" id="add_game"class="btn btn-primary btn-md"> <span class="glyphicon glyphicon-plus-sign"></span> </button> <button type="button" id="reduce_game" class="btn btn-primary btn-md"> <span class="glyphicon glyphicon-minus-sign"></span> </button>
解決方案:
增加:在tbody后直接增加自定義好的html變量,使用append方法就好了
jquery代碼如下:
<script src="../jQuery/jquery-2.2.3.min.js"></script> <!-- Bootstrap 3.3.6 --> <script src="../bootstrap/js/bootstrap.min.js"></script> <!-- Morris.js charts --> <script src="../morris/morris.min.js"></script> <!-- FastClick --> <script src="../fastclick/fastclick.js"></script> <!-- AdminLTE App --> <script src="../dist/js/app.min.js"></script> <!-- AdminLTE for demo purposes --> <script src="../dist/js/demo.js"></script> <!-- page script --> <script type="text/javascript"> function deleteCol() { alert("delete col method"); alert(this.tagName); //$(this).parent("td").parent("tr").remove(); } </script> <script> $(document).ready(function () { // 增加行 var trEle='<tr>'+ '<td><input type="number" style="width: 40px"/>'+'</td>'+ '<td><input type="date" style="width: 140px"/>'+'</td>'+ '<td><input type="text" style="width: 140px"/>'+'</td>'+ '<td><input type="text" style="width: 140px"/>'+'</td>'+ '<td><input type="number" style="width: 80px"/>'+'</td>'+ '<td><input type="text" style="width: 140px"/>'+'</td>'+ '<td><input type="number" style="width: 80px"/>'+'</td>'+ '<td><input type="text" style="width: 40px"/>'+'</td>'+ '<td><button type="button" class="btn btn-danger btn-xs" id="delete" onclick="$(this).parent('+ "'td').parent('tr').remove()"+ '">刪除</button></td></tr>' $("#add_game").click(function(e){ $("#Games_tbody").append(trEle); }); //刪除行數(shù),綁定在html中的button Click事件中了 }); </script> 問題原因: jquery沒有onclick()函數(shù),但是這里可以用(不知道為什么,因為我是菜鳥),不知道使用each()函數(shù)是否可以使用。不知道為什么直接使用下面代碼不可以用 $(".btn-danger").click(function(){ $(this).parent('td').parent(‘tr').remove(); });
只能選擇第一個,后面的就沒辦法選定了。
在解決的過程中,我借用了這篇博客
http://www.dbjr.com.cn/article/94519.htm
發(fā)現(xiàn)原來頁面上的可以實現(xiàn)刪除,但是動態(tài)增加后的行數(shù),卻無法刪除
最后還是借用了
http://bbs.csdn.net/topics/390917779
這里面的一個回答,才發(fā)現(xiàn)原來函數(shù)可以直接卸載html里面。而在增加行中,也可以使用clone函數(shù),會更加方便,也就是第二種方法。
第二種方法,選擇tr屬性,然后借用clone(),代碼如下:
$("#add_game").click(function(e){ //$("#Games_tbody").append(trEle); 第一種方法 //第二種方法 $("#Games_tbody>tr:first").clone(true).appendTo($("#Games_tbody")); });
也可以實現(xiàn)增加行,同時,點擊刪除也可以,(在上面提過的這篇博客
http://www.dbjr.com.cn/article/94519.htm
這時可以刪除,好奇怪?。?/p>
總結(jié)來說,通過拼接html來實現(xiàn)增加的行數(shù)無法實現(xiàn)刪除按鈕,解決方法是把刪除方法綁定在html中。
但是,如果,你的行數(shù)是通過clone()方法來實現(xiàn)的話,可以實現(xiàn)刪除按鈕。
以上所述是小編給大家介紹的jQuery動態(tài)增減行的實例代碼解析(推薦),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
jQuery使用removeClass方法刪除元素指定Class的方法
這篇文章主要介紹了jQuery使用removeClass方法刪除元素指定Class的方法,可實現(xiàn)針對指定元素樣式的批量刪除功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03jQuery實現(xiàn)的上傳圖片本地預(yù)覽效果簡單示例
這篇文章主要介紹了jQuery實現(xiàn)的上傳圖片本地預(yù)覽效果,結(jié)合實例形式分析了jQuery上傳圖片本地預(yù)覽所涉及的相關(guān)頁面元素屬性動態(tài)操作實現(xiàn)技巧,需要的朋友可以參考下2018-03-03jQuery中ajax獲取數(shù)據(jù)賦值給頁面的實例
下面小編就為大家分享一篇jQuery中ajax獲取數(shù)據(jù)賦值給頁面的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12jQuery Attributes(屬性)的使用(一、屬性篇)
本系列文章主要講述jQuery框架的屬性(Attributes)使用方法,文章分為:屬性篇、類篇、Html代碼篇&文本篇、值篇共4篇文章。2009-12-12jQuery插件jFade實現(xiàn)鼠標(biāo)經(jīng)過的圖片高亮其它變暗
本文給大家介紹的是一款重點突出的jQuery特效插件效果,使用jFade實現(xiàn)鼠標(biāo)經(jīng)過的圖片高亮其它變暗,非常實用,推薦給小伙伴們參考下。2015-03-03jQuery UI結(jié)合Ajax創(chuàng)建可定制的Web界面
這篇文章主要為大家詳細介紹了jQuery UI結(jié)合Ajax創(chuàng)建可定制的Web界面,如何利用Ajax和jQuery UI創(chuàng)建具有各種定制功能的高度可定制的UI,感興趣的小伙伴們可以參考一下2016-06-06