欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery動態(tài)增減行的實例代碼解析(推薦)

 更新時間:2016年12月05日 10:11:56   作者:JustCalm  
這篇文章主要介紹了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)文章

最新評論