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ù)
通過jQuery獲取checkbox選中項(xiàng)的個(gè)數(shù),需要用到j(luò)Query的size()方法或length屬性,下面的例子是通過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" />游樂場(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>
效果二(選超過三個(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常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jquery選擇器排除某個(gè)DOM元素的方法(實(shí)例演示)
- jQuery過濾選擇器:not()方法使用介紹
- jQuery選擇id屬性帶有點(diǎn)符號(hào)元素的方法
- jQuery選擇器總結(jié)之常用元素查找方法
- JQuery 選擇和過濾方法代碼總結(jié)
- JQuery的常用選擇器、過濾器、方法全面介紹
- 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插件jqGrid動(dòng)態(tài)獲取列和列字段的方法
這篇文章主要介紹了jQuery插件jqGrid動(dòng)態(tài)獲取列和列字段的方法,結(jié)合實(shí)例形式分析了表格插件jqGrid針對(duì)表格字段屬性相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
通過Jquery.cookie.js實(shí)現(xiàn)展示瀏覽網(wǎng)頁的歷史記錄超管用
本文給大家分享利用cookie插件獲取用戶瀏覽文章或者是網(wǎng)頁的歷史記錄,并把用戶最近瀏覽過的信息顯示在頁面,涉及到 jquery.cookie.js相關(guān)知識(shí),感興趣的朋友可以參考下2015-10-10
Jquery 點(diǎn)擊按鈕自動(dòng)高亮實(shí)現(xiàn)原理及代碼
拓展一個(gè)點(diǎn)擊按鈕自動(dòng)高亮的原理很簡(jiǎn)單,在點(diǎn)擊的時(shí)候給元素加上一個(gè)自定義的attr,具體實(shí)現(xiàn)祥看本文2014-04-04
jQuery實(shí)現(xiàn)圖片與文字描述左右滑動(dòng)自動(dòng)切換的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)圖片與文字描述左右滑動(dòng)自動(dòng)切換的方法,涉及jquery實(shí)現(xiàn)圖文滑動(dòng)切換效果的方法,涉及jquery針對(duì)頁面元素與樣式的相關(guān)操作技巧,需要的朋友可以參考下2015-07-07

