A example for Jquery--deal with html 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_test</title>
</head>
<script src="jquery.js"></script>
<script>
$("document").ready(function(){
//全选:
$(".btn1").click(function(){
$("[name='checkbox']").attr("checked","true");
})html

//取消全选:
$(".btn2").click(function(){
$("[name='checkbox']").removeAttr("checked");
})jquery

//选中全部奇数的checkbox:
$(".btn3").click(function(){
$("[name='checkbox']:even").attr("checked","true");
})post

//反选:(Jquery对象提供的each方法是对jQuery内部的子元素进行逐个调用)
$(".btn4").click(function(){
$("[name='checkbox']").each(function(){
if($(this).attr("checked"))
{
$(this).removeAttr("checked");
}
else
{
$(this).attr("checked","true");
}
})
})ui

//得到选中的全部值:
$(".btn5").click(function(){
var str="";
$("[name='checkbox'][checked]").each(function(){
str += $(this).val()+"rn";
})
alert(str);
})
})
</script>
<body>this

<form name="form1" method="post" action="">
   <input type="button" class="btn1" value="全选">
   <input type="button" class="btn2" value="取消全选">
   <input type="button" class="btn3" value="选中全部奇数">
   <input type="button" class="btn4" value="反选">
   <input type="button" class="btn5" value="得到选中的全部值">
   <br>
   <input class="checkbox1" type="checkbox" name="checkbox" value="checkbox1">
   checkbox1
   <input class="checkbox2" type="checkbox" name="checkbox" value="checkbox2">
   checkbox2
   <input class="checkbox3" type="checkbox" name="checkbox" value="checkbox3">
   checkbox3
   <input class="checkbox4" type="checkbox" name="checkbox" value="checkbox4">
   checkbox4
   <input class="checkbox5" type="checkbox" name="checkbox" value="checkbox5">
   checkbox5
   <input class="checkbox6" type="checkbox" name="checkbox" value="checkbox6">
   checkbox6
   <input class="checkbox7" type="checkbox" name="checkbox" value="checkbox7">
   checkbox7
   <input class="checkbox8" type="checkbox" name="checkbox" value="checkbox8">
   checkbox8
</form>
</body>
</html>
orm

相关文章
相关标签/搜索