본문 바로가기

프로그램&DB/jQuery&JS

jQuery 체크박스 전체 선택/해제 by 다크실프님

 체크박스 전체 선택 /해제
<html>
<head>
<title>title</title>
<script type = "text/javascript" src= "jquery-1.4.2.min.js" ></script>
</head>

<body>

<input type="button" id ="chkall" value="select" >
<input type="checkbox" id="chkAll" />
<input type="checkbox" name="chk" id="checkbox1" value="1" />
<input type="checkbox" name="chk" id="checkbox2" value="2" />
<input type="checkbox" name="chk" id="checkbox3" value="3" />
<input type="checkbox" name="chk" id="checkbox4" value="4" />
<input type="checkbox" name="chk" id="checkbox5" value="5" />
<input type="checkbox" name="chk" id="checkbox6" value="6" />

<script type="text/javascript">


$(function(){

$("#chkAll").click(function()
{
$bChk = $(this).is(':checked');
if(this.checked){
$(":checkbox").attr("checked", $bChk);
}
else{
$(":checkbox").attr("checked","");
}
});

});
</script>
</body>
</html>




<html>
<head>
<title>title</title>
<script type = "text/javascript" src= "jquery-1.4.2.min.js" ></script>
</head>

<body>

<input type="button" id ="selall" value="select" >
<input type="checkbox" id="chkAll" onclick="chk_all('chkAll');" />
<input type="checkbox" name="chk" id="checkbox1" value="1" />
<input type="checkbox" name="chk" id="checkbox2" value="2" />
<input type="checkbox" name="chk" id="checkbox3" value="3" />
<input type="checkbox" name="chk" id="checkbox4" value="4" />
<input type="checkbox" name="chk" id="checkbox5" value="5" />
<input type="checkbox" name="chk" id="checkbox6" value="6" />

<script type="text/javascript">

function chk_all(xVal){
var checkflag = "false";

$bChk = $("#"+xVal).is(':checked');

if(checkflag == "false"){
$(":checkbox").attr("checked", $bChk);
}
else{
$(":checkbox").attr("checked","");
}
}


</script>
</body>
</html>


[출처] http://99ani.blog.me/60104668199