本文實(shí)例講述了jquery操作checkbox的常用方法。分享給大家供大家參考,具體如下:
做系統(tǒng)的過(guò)程中,與checkbox 復(fù)選框打交道,是難免的,而且在每個(gè)系統(tǒng)中,簡(jiǎn)直是必不可少的一道菜,通常的操作有一下幾種:
1.用jquery 全部選擇checkbox
2.用jquery 全部取消checkbox
3.用jquery 實(shí)現(xiàn)checkbox 反選
4.用jquery 獲取所有已選擇checkbox的值。
現(xiàn)在將這些常用的操作總結(jié)在一個(gè)例子中,方便以后查詢,不愿意看代碼的,也可以下載源碼運(yùn)行測(cè)試。
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>yihaomen.com jquery checkbox 測(cè)試</title> <link rel="stylesheet" type="text/css" href="/static/css/global.css" rel="external nofollow" > <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(function() { /*全選或全不選*/ $("#controlall").click(function() { $('input[name="selectdivision"]').attr("checked",this.checked); }); var $subBox = $("input[name='selectdivision']"); $subBox.click(function(){ $("#controlall").attr("checked",$subBox.length == $("input[name='selectdivision']:checked").length ? true : false); }); /*反選*/ var reverseBtn=$('#reverseselect'); reverseBtn.click(function(){ $("[name='selectdivision']").each(function(){ if($(this).attr("checked")) { $(this).removeAttr("checked"); } else { $(this).attr("checked",'true'); } }) }); /*得到所有值*/ var getvalueBtn=$('#getValButton'); getvalueBtn.click(function(){ var val_array=[]; $("input:checkbox[name=selectdivision]:checked").each(function(){ val_array.push(parseInt($(this).val())); }); alert(val_array.join()); }); }); </script> </head> <body> <div> <table style="border:1px dashed #ccc;margin-top:5px;" class="crmgrid_popwindow" id="division_table"> <tr style="background-color:#F9F9F9;"> <td><input type="checkbox" id="controlall" name="controlall" />全選/全不選</td> <td> <input type="button" id="reverseselect" value="反選" /> <input type="button" id="getValButton" value="得到選擇的值" /> </td> </tr> <tr> <td><input type="checkbox" name="selectdivision" value="1"/></td> <td>1</td> </tr> <tr> <td><input type="checkbox" name="selectdivision" value="2"/></td> <td>2</td> </tr> <tr> <td><input type="checkbox" name="selectdivision" value="3"/></td> <td>3</td> </tr> <tr> <td><input type="checkbox" name="selectdivision" value="4"/></td> <td>4</td> </tr> <tr> <td><input type="checkbox" name="selectdivision" value="5"/></td> <td>5</td> </tr> <tr> <td><input type="checkbox" name="selectdivision" value="6"/></td> <td>6</td> </tr> </table> </div> </body> </html>
源碼點(diǎn)擊此處本站下載。
更多關(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