亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery獲取復(fù)選框被選中數(shù)量及判斷選擇值的方法詳解

 更新時(shí)間:2016年05月25日 10:51:07   作者:zeo  
這篇文章主要介紹了jQuery獲取復(fù)選框被選中數(shù)量及判斷選擇值的方法,結(jié)合實(shí)例形式分析了jQuery操作復(fù)選框進(jìn)行判定與統(tǒng)計(jì)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評(píng)論