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

时间:2015-10-16    点击:53   

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判断复选框是否选中实例》

谈谈Jquery中的children find 的区别有哪些
jQuery实现ctrl+enter(回车)提交表单
简单谈谈Javascript中类型的判断
学习javascript的闭包,原型,和匿名函数之旅
Javascript中的数据类型之旅
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved