jquery判断复选框是否被选中的方法


jquery 判断复选框是否选中以及如何选中的问题做一下总结。
进入正题,还是当页面有如下一组复选框的时候:

<input type="checkbox" name="fruit" value="apple" />苹果 
<input type="checkbox" name="fruit" value="orange" />橘子 
<input type="checkbox" name="fruit" value="banana" />香蕉 
<input type="checkbox" name="fruit" value="grape" />葡萄 

那么使用 Jquery 获取 name=fruit 的一组复选框的值的方法如下:

var checkVal=''; 
$("input[name='fruit']:checkbox").each(function(){ 
  if($(this).attr('checked')){ 
    checkVal+=$(this).val()+','; 
  } 
}) 
判断 name=fruit 组的复选框是否有被选中的选项:
var flag=false; 
$("input[name='fruit']:checkbox").each(function(){ 
  if($(this).attr('checked')){ 
    flag=true; 
  } 
}) 
if(flag){ 
  alert('有被选中'); 
}else{ 
  alert('没有选中任何选项'); 
} 

以上就是jquery判断复选框是否被选中的方法,希望对大家的学习有所帮助,还有用js获取的,请参考《JS判断复选框是否选中实例》


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3