本文實例講述了jQuery獲取復選框被選中數量及判斷選擇值的方法。分享給大家供大家參考,具體如下:
獲取復選框被選中值
<input type="button" id="btn5" value="獲得選中的所有值"> <input type="text" name="dd" id="dd" size="50" /> $("#btn5").click(function(){ var str=""; $("[name='checkbox'][checked]").each(function(){ str+=$(this).val()+","; }) $("#dd").val(str) })
JQuery獲取被選中復選框checkbox的個數
通過jQuery獲取checkbox選中項的個數,需要用到jQuery的size()方法或length屬性,下面的例子是通過length屬性獲得checkbox選中項的個數
<ul> <li><input type="checkbox" name="test" />看電視</li> <li><input type="checkbox" name="test" />看電影</li> <li><input type="checkbox" name="test" />上網</li> <li><input type="checkbox" name="test" />爬山</li> <li><input type="checkbox" name="test" />游樂場</li> <li><input type="checkbox" name="test" />逛街</li> <li><input type="checkbox" name="test" />聚會</li> </ul> <p> <input type="button" id="count" value="有多少CheckBox被選中了?" /> <script type="text/javascript"> $(document).ready(function(){ $('input[type=checkbox]').click(function(){ $(this).attr('disabled','disabled'); if($("input[name='test']:checked").length >= 3) { $("input[name='test']").attr('disabled','disabled'); } }); $("#count").click(function(){ $('input').live('click',function(){ alert($('input:checked').length); }); }) }) </script>
效果二(選超過三個做彈窗提示):
<script type="text/javascript"> $('input[type=checkbox]').click(function(){ if($("input[name='test']:checked").length >= 4) { $(this).removeAttr("checked"); alert("最多選3個!")} }); </script>
jquery如何判斷checkbox(復選框)是否被選中/全選/返選/取消全選:
在html 如果一個復選框被選中 是 checked="checked"。
但是我們如果用jquery alert($("#id").attr("checked")) 會提示您是true而不是checked
所以很多朋友判斷:
if($("#id").attr("checked")=="true")
這個是錯誤的,其實應該是:
if($("#id").attr("checked")==true)
例子里面包括了一下幾個功能。
<input type="button" id="btn1" value="全選"> <input type="button" id="btn2" value="取消全選"> <input type="button" id="btn3" value="選中所有奇數"> <input type="button" id="btn4" value="反選"> <input type="button" id="btn5" value="獲得選中的所有值">
代碼
<script src="http://www.gimoo.net/t/1902/js/jquery-1.6.min.js" type="text/javascript"></script> <script type="text/javascript"> jQuery(function($){ //全選 $("#btn1").click(function(){ $("input[name='checkbox']").attr("checked","true"); }) //取消全選 $("#btn2").click(function(){ $("input[name='checkbox']").removeAttr("checked"); }) //選中所有基數 $("#btn3").click(function(){ $("input[name='checkbox']:even").attr("checked","true"); }) //選中所有偶數 $("#btn6").click(function(){ $("input[name='checkbox']:odd").attr("checked","true"); }) //反選 $("#btn4").click(function(){ $("input[name='checkbox']").each(function(){ if($(this).attr("checked")) { $(this).removeAttr("checked"); } else { $(this).attr("checked","true"); } }) }) //或許選擇項的值 var aa=""; $("#btn5").click(function(){ $("input[name='checkbox']:checkbox:checked").each(function(){ aa+=$(this).val() }) document.write(aa); }) }) </script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" id="btn1" value="全選"> <input type="button" id="btn2" value="取消全選"> <input type="button" id="btn3" value="選中所有奇數"> <input type="button" id="btn6" value="選中所有偶數"> <input type="button" id="btn4" value="反選"> <input type="button" id="btn5" value="獲得選中的所有值"> <br> <input type="checkbox" name="checkbox" value="checkbox1"> checkbox1 <input type="checkbox" name="checkbox" value="checkbox2"> checkbox2 <input type="checkbox" name="checkbox" value="checkbox3"> checkbox3 <input type="checkbox" name="checkbox" value="checkbox4"> checkbox4 <input type="checkbox" name="checkbox" value="checkbox5"> checkbox5 <input type="checkbox" name="checkbox" value="checkbox6"> checkbox6 <input type="checkbox" name="checkbox" value="checkbox7"> checkbox7 <input type="checkbox" name="checkbox" value="checkbox8"> checkbox8 </div> </form>
jquery 循環讀取checkbox值
$("input[type=checkbox][checked]").each(function(){ //由于復選框一般選中的是多個,所以可以循環輸出 alert($(this).val()); });
PS:上面的代碼排版比較粗糙,小編這里為省事就不重新排版了,推薦幾款排版工具供大家參考使用:
在線JavaScript代碼美化、格式化工具:
http://tools.gimoo.net/code/js
JavaScript代碼美化/壓縮/格式化/加密工具:
http://tools.gimoo.net/code/jscompress
json代碼在線格式化/美化/壓縮/編輯/轉換工具:
http://tools.gimoo.net/code/jsoncodeformat
在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.gimoo.net/code/json
更多關于jQuery相關內容可查看本站專題:《jquery中Ajax用法總結》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》、《jquery選擇器用法總結》及《jQuery常用插件及用法總結》
希望本文所述對大家jQuery程序設計有所幫助。