最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年12月05日 10:11:56   作者:JustCalm  
這篇文章主要介紹了jQuery動(dòng)態(tài)增減行的實(shí)例代碼解析,本文圖文并茂給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下

先給大家展示下效果圖:

這是沒(méi)有增加時(shí)的界面:

這里寫圖片描述 

增加后的界面:

這里寫圖片描述 

刪除后的界面:

這里寫圖片描述

原因分析:

不擅長(zhǎng)jquery和JavaScript

場(chǎng)景:

代碼如下:

<table class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">輪次</th>
<th style="width: 100%">比賽時(shí)間</th>
<th>比賽場(chǎng)地</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>

問(wèn)題原因:

jquery沒(méi)有onclick()函數(shù),但是這里可以用(不知道為什么,因?yàn)槲沂遣锁B),不知道使用each()函數(shù)是否可以使用。不知道為什么直接使用下面代碼不可以用

$(".btn-danger").click(function(){
$(this).parent('td').parent(‘tr').remove();
});

只能選擇第一個(gè),后面的就沒(méi)辦法選定了。

在解決的過(guò)程中,我借用了這篇博客

http://www.fzitv.net/article/94519.htm

發(fā)現(xiàn)原來(lái)頁(yè)面上的可以實(shí)現(xiàn)刪除,但是動(dòng)態(tài)增加后的行數(shù),卻無(wú)法刪除

最后還是借用了

http://bbs.csdn.net/topics/390917779

這里面的一個(gè)回答,才發(fā)現(xiàn)原來(lái)函數(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)擊刪除也可以,(在上面提過(guò)的這篇博客

http://www.fzitv.net/article/94519.htm

這時(shí)可以刪除,好奇怪!)

總結(jié)來(lái)說(shuō),通過(guò)拼接html來(lái)實(shí)現(xiàn)增加的行數(shù)無(wú)法實(shí)現(xiàn)刪除按鈕,解決方法是把刪除方法綁定在html中。

但是,如果,你的行數(shù)是通過(guò)clone()方法來(lái)實(shí)現(xiàn)的話,可以實(shí)現(xiàn)刪除按鈕。

以上所述是小編給大家介紹的jQuery動(dòng)態(tài)增減行的實(shí)例代碼解析(推薦),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

武胜县| 航空| 祁阳县| 毕节市| 师宗县| 新密市| 缙云县| 巴中市| 文安县| 潼南县| 绥化市| 沂南县| 天镇县| 长兴县| 唐海县| 屯昌县| 独山县| 河津市| 奈曼旗| 昌宁县| 洞头县| 郁南县| 丁青县| 萨嘎县| 万源市| 依安县| 方城县| 肃北| 马关县| 昌图县| 芜湖市| 都匀市| 敦煌市| 五莲县| 宁安市| 临沭县| 保德县| 安宁市| 邮箱| 淮滨县| 衡阳县|