用jquery實現的全選、全不選(實用代碼)


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
div{
width:200px;
height:200px;
background-color:#0FF;
}
.content{
width:200px;
height:200px;
background-color:#CF3;
}
</style>

<title>jquery實現的全選,全不選</title>

//引進js文件

<script language="javascript" type="text/javascript" src="../../jquery/jquery.js"></script>


<script language="javascript" type="text/javascript">


//使用jquery加載事件
$(document).ready(function (){

//設置全選
$("#all-yes").click(function (){
$("input[name=fav]").attr("checked","checked");
});
//設置全不選
$("#all-no").click(function (){
$("input[name=fav]").attr("checked",false);
});
//設置反選
$("#fanxuan").click(function (){
//獲得所有的複選框
$("input[name=fav]").each(function(){
//獲得每一個複選框的選中狀態,如果已選中則改爲爲選狀態
//如果未選中則改爲選中狀態
//this是dom對象,
if(this.checked){
//封裝成jquery對象,並且設置爲checked的值
$(this).attr("checked",false);
}else{
$(this).attr("checked","checked");
}
});
});
$("#isAll").click(function(){
if(this.checked){
$("input[name=fav]").attr("checked","checked");
}else{
$("input[name=fav]").attr("checked",false);
         }
});
});
</script>
</head>
<body>
<form action="" method="post"> 
請選擇你的愛好:
<br>
<input type="checkbox" id="isAll" value="全選/全不選" />全選/全不選
<br />
<input type="checkbox" name="fav" value="看小說" />看小說
<input type="checkbox" name="fav" value="吃" />吃
<input type="checkbox" name="fav" value="喝" />喝
<input type="checkbox" name="fav" value="不上課" />不上課
<br />
<input id="all-yes" type="button" value="全選" />
<input id="all-no" type="button" value="全不選" />
<input id="fanxuan" type="button" value="反選" />
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章