jQuery獲取復選框被選中數(shù)量及判斷選擇值的方法詳解
來源:易賢網(wǎng) 閱讀:830 次 日期:2016-06-23 14:22:49
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery獲取復選框被選中數(shù)量及判斷選擇值的方法詳解”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了jQuery獲取復選框被選中數(shù)量及判斷選擇值的方法,結合實例形式分析了jQuery操作復選框進行判定與統(tǒng)計的相關技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了jQuery獲取復選框被選中數(shù)量及判斷選擇值的方法。分享給大家供大家參考,具體如下:

獲取復選框被選中值

<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的個數(shù)

通過jQuery獲取checkbox選中項的個數(shù),需要用到jQuery的size()方法或length屬性,下面的例子是通過length屬性獲得checkbox選中項的個數(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" />游樂場</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="選中所有奇數(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"); 

}) 

}) 

//或許選擇項的值 

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(){

//由于復選框一般選中的是多個,所以可以循環(huán)輸出 

alert($(this).val()); 

});

希望本文所述對大家jQuery程序設計有所幫助。

更多信息請查看網(wǎng)絡編程

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網(wǎng)