本文實(shí)例講述了jQuery實(shí)現(xiàn)checkbox全選、反選及刪除等操作的方法。分享給大家供大家參考,具體如下:
運(yùn)行效果:
1.list.html
說(shuō)明:用checkbox數(shù)組Check[]存放每一行的ID值
<div id="con"> <table width="100%" cellspacing="1" cellpadding="0"> <tr> <th><input id="CheckAll" name='CheckAll' type='checkbox'></th> <th>ID</th> <th>Name</th> <th>Date</th> </tr> <tr> <td><input id='Check[]' name='Check[]' type='checkbox' value="1"></td> <td>10001</td> <td>XXX</td> <td>2015-12-01</td> </tr> <tr> <td><input id='Check[]' name='Check[]' type='checkbox' value="2"></td> <td>10002</td> <td>XXX</td> <td>2015-12-02</td> </tr> <tr> <td><input id='Check[]' name='Check[]' type='checkbox' value="3"></td> <td>10003</td> <td>XXX</td> <td>2015-12-03</td> </tr> </table> <div id="bottom"> <input id="Delete" name="Delete" type="button" value=" 刪 除 " class="btn btn-danger radius"/> </div> </div>
2.功能:全選/全不選
說(shuō)明:如果需要這里還可以根據(jù)選擇結(jié)果顯示/隱藏【刪除】等按鈕
//全選/全不選 $("#CheckAll").bind("click",function(){ $("input[name='Check[]']").prop("checked",this.checked); //顯示刪除按鈕 /*if(this.checked == true){ $("input[name='Delete'").css("display",'block'); }else{ $("input[name='Delete'").css("display",'none'); }*/ });
3.功能:批量刪除
說(shuō)明:需注意的是在傳值的的時(shí)候,要把獲取的數(shù)組checkData轉(zhuǎn)化成字符串checkData.toString()
//批量刪除 $("#Delete").click(function(){ if(confirm('確定要?jiǎng)h除所選嗎?')){ var checks = $("input[name='Check[]']:checked"); if(checks.length == 0){ alert('未選中任何項(xiàng)!');return false;} //將獲取的值存入數(shù)組 var checkData = new Array(); checks.each(function(){ checkData.push($(this).val()); }); $.get("<{spUrl c=order a=delete}>",{Check:checkData.toString()},function(result){ if(result = true){ window.location.reload();}}); } } });
實(shí)際應(yīng)用中往往不僅僅只有批量刪除一個(gè)操作,通常還有其它的批量操作,我們可以對(duì)上面代碼優(yōu)化整合一下,將通用的功能放在一個(gè)文件中。
通用文件:jquery.ready.js
說(shuō)明:因?yàn)檫@一部分較為通用可以放在一個(gè)公共的js文件里面,獲取checkbox值可以寫(xiě)成一個(gè)變量函數(shù)
//獲取被選中checkbox值 var checked = function(){ var checks = $("input[name='Check[]']:checked"); if(checks.length == 0){ alert('未選中任何項(xiàng)!');return false;} var checkData = new Array(); checks.each(function(){ checkData.push($(this).val()); }); return checkData; }; //全選/全不選 $("#CheckAll").bind("click",function(){ $("input[name='Check[]']").prop("checked",this.checked); //顯示刪除按鈕 /*if(this.checked == true){ $("input[name='Delete'").css("display",'block'); }else{ $("input[name='Delete'").css("display",'none'); }*/ });
list.js
//批量刪除 $("#Delete").click(function(){ if(val = checked()){ if(confirm('確定要?jiǎng)h除所選嗎?')){ $.get("<{spUrl c=order a=delete}>",{Check:val.toString()},function(result){ if(result = true){ window.location.reload();}}); } } }); //批量操作...
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery頁(yè)面元素操作技巧匯總》、《jQuery常見(jiàn)事件用法與技巧總結(jié)》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com