jQuery動(dòng)態(tài)增減行的實(shí)例代碼解析(推薦)
先給大家展示下效果圖:
這是沒有增加時(shí)的界面:
增加后的界面:
刪除后的界面:

原因分析:
不擅長jquery和JavaScript
場景:
代碼如下:
<table class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">輪次</th>
<th style="width: 100%">比賽時(shí)間</th>
<th>比賽場地</th>
<th>主隊(duì)</th>
<th>主隊(duì)得分</th>
<th>客隊(duì)</th>
<th>客隊(duì)得分</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ù),但是這里可以用(不知道為什么,因?yàn)槲沂遣锁B),不知道使用each()函數(shù)是否可以使用。不知道為什么直接使用下面代碼不可以用
$(".btn-danger").click(function(){
$(this).parent('td').parent(‘tr').remove();
});
只能選擇第一個(gè),后面的就沒辦法選定了。
在解決的過程中,我借用了這篇博客
http://www.dhdzp.com/article/94519.htm
發(fā)現(xiàn)原來頁面上的可以實(shí)現(xiàn)刪除,但是動(dòng)態(tài)增加后的行數(shù),卻無法刪除
最后還是借用了
http://bbs.csdn.net/topics/390917779
這里面的一個(gè)回答,才發(fā)現(xiàn)原來函數(shù)可以直接卸載html里面。而在增加行中,也可以使用clone函數(shù),會(huì)更加方便,也就是第二種方法。
第二種方法,選擇tr屬性,然后借用clone(),代碼如下:
$("#add_game").click(function(e){
//$("#Games_tbody").append(trEle); 第一種方法
//第二種方法 $("#Games_tbody>tr:first").clone(true).appendTo($("#Games_tbody"));
});
也可以實(shí)現(xiàn)增加行,同時(shí),點(diǎn)擊刪除也可以,(在上面提過的這篇博客
http://www.dhdzp.com/article/94519.htm
這時(shí)可以刪除,好奇怪!)
總結(jié)來說,通過拼接html來實(shí)現(xiàn)增加的行數(shù)無法實(shí)現(xiàn)刪除按鈕,解決方法是把刪除方法綁定在html中。
但是,如果,你的行數(shù)是通過clone()方法來實(shí)現(xiàn)的話,可以實(shí)現(xiàn)刪除按鈕。
以上所述是小編給大家介紹的jQuery動(dòng)態(tài)增減行的實(shí)例代碼解析(推薦),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
bootstrap table 服務(wù)器端分頁例子分享
這篇文章主要介紹了bootstrap table 服務(wù)器端分頁例子分享,需要的朋友可以參考下2015-02-02
jQuery實(shí)現(xiàn)隔行變色的方法分析(對(duì)比原生JS)
這篇文章主要介紹了jQuery實(shí)現(xiàn)隔行變色的方法,結(jié)合實(shí)例形式對(duì)比原生JS實(shí)現(xiàn)隔行變色的相關(guān)技巧,給出了2種jQuery實(shí)現(xiàn)隔行變色的方法,需要的朋友可以參考下2016-11-11
jQuery獲取選中內(nèi)容及設(shè)置元素屬性的方法
這篇文章主要介紹了jQuery獲取選中內(nèi)容及設(shè)置元素屬性的方法,需要的朋友可以參考下2014-07-07
jQuery 選擇器用法實(shí)例分析【prev + next】
這篇文章主要介紹了jQuery 選擇器用法,結(jié)合實(shí)例形式分析了jQuery選擇器prev 與 next基本功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05
自動(dòng)設(shè)置iframe大小的jQuery代碼
自動(dòng)設(shè)置iframe的寬度在應(yīng)用中還是比較廣泛的,本文使用jquery實(shí)現(xiàn)一下,感興趣的朋友可以參考下2013-09-09
jquery選擇器排除某個(gè)DOM元素的方法(實(shí)例演示)
這篇文章主要介紹了jquery選擇器排除某個(gè)DOM元素的方法,也就是在選中的一些元素中,過濾一些不需要的,使用jquery not選擇器實(shí)現(xiàn),需要的朋友可以參考下2014-04-04

