這篇文章主要介紹了JQuery選中checkbox方法代碼實例(全選、反選、全不選),本文直接給出代碼實例,需要的朋友可以參考下
1、checkbox list選擇
代碼:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { // 全選 $("#btnCheckAll").bind("click", function () { $("[name = chkItem]:checkbox").attr("checked", true); }); // 全不選 $("#btnCheckNone").bind("click", function () { $("[name = chkItem]:checkbox").attr("checked", false); }); // 反選 $("#btnCheckReverse").bind("click", function () { $("[name = chkItem]:checkbox").each(function () { $(this).attr("checked", !$(this).attr("checked")); }); }); // 全不選 $("#btnSubmit").bind("click", function () { var result = new Array(); $("[name = chkItem]:checkbox").each(function () { if ($(this).is(":checked")) { result.push($(this).attr("value")); } }); alert(result.join(",")); }); }); </script> </head> <body> <div> <input name="chkItem" type="checkbox" value="今日話題" />今日話題 <input name="chkItem" type="checkbox" value="視覺焦點" />視覺焦點 <input name="chkItem" type="checkbox" value="財經" />財經 <input name="chkItem" type="checkbox" value="汽車" />汽車 <input name="chkItem" type="checkbox" value="科技" />科技 <input name="chkItem" type="checkbox" value="房產" />房產 <input name="chkItem" type="checkbox" value="旅游" />旅游 </div> <div> <input id="btnCheckAll" type="button" value="全選" /> <input id="btnCheckNone" type="button" value="全不選" /> <input id="btnCheckReverse" type="button" value="反選" /> <input id="btnSubmit" type="button" value="提交" /> </div> </body> </html>2、checkbox table選中
效果圖:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> table { border-collapse: collapse; } td { border: 1px solid #ccc; } </style> <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { // chkAll全選事件 $("#chkAll").bind("click", function () { $("[name = chkItem]:checkbox").attr("checked", this.checked); }); // chkItem事件 $("[name = chkItem]:checkbox").bind("click", function () { var $chk = $("[name = chkItem]:checkbox"); $("#chkAll").attr("checked", $chk.length == $chk.filter(":checked").length); }) }); </script> </head> <body> <table id="tb"> <thead> <tr>