jquery判断复选框是否被选中

 
更多

不久前总结了一下关于 javascript 如何判断复选框是否选中的问题,由于最近使用 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('没有选中任何选项');
}
打赏

本文固定链接: https://www.cxy163.net/archives/4622 | 绝缘体

该日志由 绝缘体.. 于 2013年05月23日 发表在 javascript, 科技企业, 编程语言, 苹果 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: jquery判断复选框是否被选中 | 绝缘体
关键字: , , , ,

jquery判断复选框是否被选中:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter