JQuery判断checkbox是否选中及其它复选框操作方法合集
一、jquery判断checkbox是否选中及改变checkbox状态
jquery判断checked的三种方法:
.attr('checked): //看版本1.6+返回:”checked”或”undefined”;1.5-返回:true或false
.prop('checked')://16+:true/false
.is(':checked'): //所有版本:true/false//别忘记冒号哦
jquery赋值checked的几种写法:
所有的jquery版本都可以这样赋值:
//$("#cb1").attr("checked","checked");
//$("#cb1").attr("checked",true);
jquery1.6+:prop的4种赋值:
//$("#cb1″).prop("checked",true);//很简单就不说了哦
//$("#cb1″).prop({checked:true});//map键值对
//$("#cb1″).prop("checked",function(){
returntrue;//函数返回true或false
});
//记得还有这种哦:$("#cb1″).prop("checked","checked");
二、jquery如何判断checkbox(复选框)是否被选中
谁都知道在html如果一个复选框被选中是checked="checked"。
但是我们如果用jqueryalert($("#id").attr("checked"))会提示您是true而不是checked
所以很多朋友判断 if($("#id").attr("checked")=="true")这个是错误的,其实应该是if($("#id").attr("checked")==true)
例子里面包括了一下几个功能。
<inputtype="button"id="btn1"value="全选"> <inputtype="button"id="btn2"value="取消全选"> <inputtype="button"id="btn3"value="选中所有奇数"> <inputtype="button"id="btn4"value="反选"> <inputtype="button"id="btn5"value="获得选中的所有值">
代码
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<TITLE>NewDocument</TITLE>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
<SCRIPTLANGUAGE="JavaScript"src="http://www.cnjquery.com/demo/jquery.js"></script>
<SCRIPTLANGUAGE="JavaScript">
<!--
$("document").ready(function(){
$("#btn1").click(function(){
$("[name='checkbox']").attr("checked",'true');//全选
})
$("#btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");//取消全选
})
$("#btn3").click(function(){
$("[name='checkbox']:even").attr("checked",'true');//选中所有奇数
})
$("#btn4").click(function(){
$("[name='checkbox']").each(function(){
if($(this).attr("checked"))
{
$(this).removeAttr("checked");
}
else
{
$(this).attr("checked",'true');
}
})
})
$("#btn5").click(function(){
varstr="";
$("[name='checkbox'][checked]").each(function(){
str+=$(this).val()+""r"n";
//alert($(this).val());
})
alert(str);
})
})
//-->
</SCRIPT>
</HEAD>
<BODY>
<formname="form1"method="post"action="">
<inputtype="button"id="btn1"value="全选">
<inputtype="button"id="btn2"value="取消全选">
<inputtype="button"id="btn3"value="选中所有奇数">
<inputtype="button"id="btn4"value="反选">
<inputtype="button"id="btn5"value="获得选中的所有值">
<br>
<inputtype="checkbox"name="checkbox"value="checkbox1">
checkbox1
<inputtype="checkbox"name="checkbox"value="checkbox2">
checkbox2
<inputtype="checkbox"name="checkbox"value="checkbox3">
checkbox3
<inputtype="checkbox"name="checkbox"value="checkbox4">
checkbox4
<inputtype="checkbox"name="checkbox"value="checkbox5">
checkbox5
<inputtype="checkbox"name="checkbox"value="checkbox6">
checkbox6
<inputtype="checkbox"name="checkbox"value="checkbox7">
checkbox7
<inputtype="checkbox"name="checkbox"value="checkbox8">
checkbox8
</form>
三、jquery判断checkbox是否被选中
在html的checkbox里,选中的话会有属性checked="checked"。
如果用一个checkbox被选中,alert这个checkbox的属性"checked"的值alert($"#xxx".attr("checked")),会打印出"true"",而不是checked"!
如果没被选中,打印出的是"undefined"。
不要尝试去做这样的判断:if($"#xxx".attr("checked")=="true")或者if($"#xxx".attr("checked")=='checked')
应该是if($("#checkbox1").attr("checked")==true)
全选和全不选函数
functioncheckAll(){
if($("#checkbox1").attr("checked")==true){
$("input[name='xh']").each(function(){
$(this).attr('checked',true);
});
}else{
$("input[name='xh']").each(function(){
$(this).attr('checked',false);
});
}
}
四、JQuery判断checkbox是否选中,checkbox全选,获取checkbox选中值
JQuery是一个非常容易上手的框架,但是有很多东西需要我们深入学习的。
判断checkbox是否被选中网上有选多种写法,这里有一种方法,个人觉得
比较方便。
因为比较简单,没什么技术含量,直接代码
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>JQuery判断checkbox是否选中,checkbox全选,获取checkbox选中值</title>
<scripttype="text/javascript"language="javascript"src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<scripttype="text/javascript">
$(function(){
/*------------
全选/全不选
------------*/
$('#cboxchecked').click(function(){
//判断apple是否被选中
varbischecked=$('#cboxchecked').is(':checked');
varfruit=$('input[name="fruit"]');
bischecked?fruit.attr('checked',true):fruit.attr('checked',false);
});
/*-------------
获取选中值
-------------*/
$('#btn_submit').submit(function(){
$('input[name="fruit"]:checked').each(function(){
varsfruit=$(this).val();
alert(sfruit);
});
returnfalse;
});
})
</script>
</head>
<body>
<formaction="">
<inputtype="checkbox" id="cboxchecked"/>
<labelfor="cboxchecked">全选/全不选</label>
<br/>
<br/>
<inputtype="checkbox" id="cboxapple"name="fruit"value="apple"/>
<labelfor="apple">Apple</label>
<inputtype="checkbox" id="cboxorange"name="fruit"value="orange"/>
<labelfor="orange">Orange</label>
<inputtype="checkbox" id="cboxbanana"name="fruit"value="banana"/>
<labelfor="banana">Banana</label>
<inputtype="checkbox" id="cboxgrapes"name="fruit"value="grapes"/>
<labelfor="grapes">Grapes</label>
<br/>
<br/>
<inputtype="submit"id="btn_submit"value="submit"/>
</form>
</body>
</html>