jquery checkbox radio 標簽 選中的3種方法


 

張映 發表於 2013-07-16

分類目錄: js/jquery

標簽:checkbox, jquery, radio, 選中

jquery 很靈活,checkbox radio標簽選中的方法有很多,在這兒就例舉三個常用的方法。

 

一,測試html

  1. <div style="margin-top:150px;">  
  2.     <input type='checkbox' name='test[]' class='checkbox' value='checkbox1'>checkbox1  
  3.     <input type='checkbox' name='test[]' class='checkbox' value='checkbox2'>checkbox2  
  4.     <input type='checkbox' name='test[]' class='checkbox' value='checkbox3'>checkbox3  
  5. </div>  
  6. <div style="margin-top:50px;">  
  7.     <input type="radio" name="test1" value = "radio1" class='radio' >radio1  
  8.     <input type="radio" name="test1" value = "radio2" class='radio' >radio2  
  9.     <input type="radio" name="test1" value = "radio3" class='radio' >radio3  
  10. </div>  

二,checkbok,radio選中

方法1,

  1. //這二個方法屬於一類  
  2. $('.checkbox:checked');  
  3. $('input[type^=checkbox]:checked');  

方法2,

  1. $('.checkbox').filter(':checked');  

方法3

  1. $('.checkbox').each(function(){  
  2.     if($(this).is(":checked")){  
  3.         alert($(this).val()+": is checked");  
  4.     }else{  
  5.         alert($(this).val()+": is not checked");  
  6.     }  
  7. })  

將上面的.checkbox換成.radio,就可以判斷radio標簽的選中了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM