jQuery獲取復(fù)選框被選中數(shù)量及判斷選擇值的方法詳解
本文實(shí)例講述了jQuery獲取復(fù)選框被選中數(shù)量及判斷選擇值的方法。分享給大家供大家參考,具體如下:
獲取復(fù)選框被選中值
<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獲取被選中復(fù)選框checkbox的個(gè)數(shù)
通過(guò)jQuery獲取checkbox選中項(xiàng)的個(gè)數(shù),需要用到j(luò)Query的size()方法或length屬性,下面的例子是通過(guò)length屬性獲得checkbox選中項(xiàng)的個(gè)數(shù)
<ul> <li><input type="checkbox" name="test" />看電視</li> <li><input type="checkbox" name="test" />看電影</li> <li><input type="checkbox" name="test" />上網(wǎng)</li> <li><input type="checkbox" name="test" />爬山</li> <li><input type="checkbox" name="test" />游樂(lè)場(chǎng)</li> <li><input type="checkbox" name="test" />逛街</li> <li><input type="checkbox" name="test" />聚會(huì)</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>
效果二(選超過(guò)三個(gè)做彈窗提示):
<script type="text/javascript"> $('input[type=checkbox]').click(function(){ if($("input[name='test']:checked").length >= 4) { $(this).removeAttr("checked"); alert("最多選3個(gè)!")} }); </script>
jquery如何判斷checkbox(復(fù)選框)是否被選中/全選/返選/取消全選:
在html 如果一個(gè)復(fù)選框被選中 是 checked="checked"。
但是我們如果用jquery alert($("#id").attr("checked")) 會(huì)提示您是true而不是checked
所以很多朋友判斷:
if($("#id").attr("checked")=="true")
這個(gè)是錯(cuò)誤的,其實(shí)應(yīng)該是:
if($("#id").attr("checked")==true)
例子里面包括了一下幾個(gè)功能。
<input type="button" id="btn1" value="全選"> <input type="button" id="btn2" value="取消全選"> <input type="button" id="btn3" value="選中所有奇數(shù)"> <input type="button" id="btn4" value="反選"> <input type="button" id="btn5" value="獲得選中的所有值">
代碼
<script src="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"); }) //選中所有基數(shù) $("#btn3").click(function(){ $("input[name='checkbox']:even").attr("checked","true"); }) //選中所有偶數(shù) $("#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"); } }) }) //或許選擇項(xiàng)的值 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="選中所有奇數(shù)"> <input type="button" id="btn6" value="選中所有偶數(shù)"> <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 循環(huán)讀取checkbox值
$("input[type=checkbox][checked]").each(function(){ //由于復(fù)選框一般選中的是多個(gè),所以可以循環(huán)輸出 alert($(this).val()); });
PS:上面的代碼排版比較粗糙,小編這里為省事就不重新排版了,推薦幾款排版工具供大家參考使用:
在線JavaScript代碼美化、格式化工具:
http://tools.jb51.net/code/js
JavaScript代碼美化/壓縮/格式化/加密工具:
http://tools.jb51.net/code/jscompress
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json
更多關(guān)于jQuery相關(guān)內(nèi)容可查看本站專題:《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jquery選擇器排除某個(gè)DOM元素的方法(實(shí)例演示)
- jQuery過(guò)濾選擇器:not()方法使用介紹
- jQuery選擇id屬性帶有點(diǎn)符號(hào)元素的方法
- jQuery選擇器總結(jié)之常用元素查找方法
- JQuery 選擇和過(guò)濾方法代碼總結(jié)
- JQuery的常用選擇器、過(guò)濾器、方法全面介紹
- jquery $(this).attr $(this).val方法使用介紹
- 詳談jQuery中的this和$(this)
- jQuery中$this和$(this)的區(qū)別介紹(一看就懂)
- JQuery this 和 $(this) 的區(qū)別
- jQuery 中$(this).index與$.each的使用指南
- jQuery 選擇方法及$(this)用法實(shí)例分析
相關(guān)文章
jQuery插件開發(fā)精品教程(讓你的jQuery更上一個(gè)臺(tái)階)
這篇jQuery插件開發(fā)教程是小編見(jiàn)過(guò)的最詳細(xì)的了,每個(gè)解說(shuō)都很好,對(duì)于想做增強(qiáng)插件的朋友確實(shí)不錯(cuò)的參考資料,特分享下,方便需要的朋友2015-11-11jQuery中實(shí)現(xiàn)動(dòng)畫效果的基本操作介紹
本篇文章小編將為大家介紹,在jQuery中實(shí)現(xiàn)動(dòng)畫效果的基本操作介紹,需要的朋友可以參考一下2013-04-0430個(gè)最佳jQuery Lightbox效果插件分享
Lightbox 應(yīng)該是最流行的圖片瀏覽效果了,常具有功能包括:自動(dòng)根據(jù)窗口的大小縮放圖片,模式窗口,幻燈片方式播放,內(nèi)容預(yù)加載,漸變等效果。2011-04-04jQuery scroll事件實(shí)現(xiàn)監(jiān)控滾動(dòng)條分頁(yè)示例
這篇文章主要介紹了jQuery scroll事件實(shí)現(xiàn)監(jiān)控滾動(dòng)條分頁(yè)簡(jiǎn)單示例,使用ajax加載,同時(shí)介紹了(document).height()與$(window).height()的區(qū)別,需要的朋友可以參考下2014-04-04jQuery倒計(jì)時(shí)代碼(超簡(jiǎn)單)
本文給大家分享一段超簡(jiǎn)單的代碼基于jquery實(shí)現(xiàn)倒計(jì)時(shí)功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-02-02jQuery form插件之a(chǎn)jaxForm()和ajaxSubmit()的可選參數(shù)項(xiàng)對(duì)象
這篇文章主要介紹了jQuery form插件之a(chǎn)jaxForm()和ajaxSubmit()的可選參數(shù)項(xiàng)對(duì)象的相關(guān)資料,需要的朋友可以參考下2016-01-01在jQuery1.5中使用deferred對(duì)象 著放大鏡看Promise
在那篇經(jīng)典的關(guān)于jQuery1.5中Deferred使用方法介紹的文章中(譯文見(jiàn)這里),有下面一段描述2011-03-03