本文實例講述了jquery操作checkbox的常用方法。分享給大家供大家參考,具體如下:
做系統的過程中,與checkbox 復選框打交道,是難免的,而且在每個系統中,簡直是必不可少的一道菜,通常的操作有一下幾種:
1.用jquery 全部選擇checkbox
2.用jquery 全部取消checkbox
3.用jquery 實現checkbox 反選
4.用jquery 獲取所有已選擇checkbox的值。
現在將這些常用的操作總結在一個例子中,方便以后查詢,不愿意看代碼的,也可以下載源碼運行測試。
<!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 測試</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>
源碼點擊此處本站下載。
更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結》、《jQuery擴展技巧總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com