JQuery获取checkbox值,checkbox全选,操作checkbox

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQuery 操作 checkbox</title>
<script type="text/javascript" src="../jquery-1.6.1.min.js"></script>
<script type="text/javascript" language="javascript">
/*------判断radio是否有选中,获取选中的值--------*/
$(function(){
/*-----全选-----*/
$('#rdo_checked_all').click(function(){
var is_checked=$('#rdo_checked_all').attr('checked');
if(is_checked=="checked"){
$('input[name=color]').attr('checked',true);
}
else{
$('input[name=color]').attr('checked',false);
}
});
$('#btn_getValue').click(function(){
//这里我们获取input为checkbox,name为color而且是选中的,很容易理解不是?
$('input:checkbox[name=color][checked]').each(function(){
var scolor=$(this).val();
alert(scolor);
});
});
});//document ready
</script>
</head>

<body>
<br />
<input type="checkbox" name="display" id="rdo_checked_all"/><label for="rdo_checked_all" >全选</label><br />
<label>红:<input type="checkbox" name="color" value="red" id="rdo_red"/></label>
<label>黄:<input type="checkbox" name="color" value="yellow" id="rdo_yellow"/></label>
<label>蓝:<input type="checkbox" name="color" value="blue" id="rdo_blue"/></label>
<label>绿:<input type="checkbox" name="color" value="green" id="rdo_green"/></label>
<label>黑:<input type="checkbox" name="color" value="black" id="rdo_black"/></label><br />
<input type="button" id="btn_getValue" value="获取选中值" />
</body>
</html>
posted @ 2011-12-27 00:15  Lee zhang  阅读(198)  评论(0编辑  收藏  举报