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标签的选中了

posted on 2017-03-30 23:08  熊熊之火  阅读(2067)  评论(0编辑  收藏  举报

导航