checkbox 전체 선택 기능 구현

5293 단어 JavaScriptjquery
하나.JQuery 전체 선택 기능 구현:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>checkbox</title>
</head>
<body>
    <input type="checkbox" name="btn" id="btn"/><label for="btn">  /   </label><br/>
    <input type="checkbox" name="choose" id="checkbox1"/><label for="checkbox1">  1</label><br/>
    <input type="checkbox" name="choose" id="checkbox2"/><label for="checkbox2">  2</label><br/>
    <input type="checkbox" name="choose" id="checkbox3"/><label for="checkbox3">  3</label><br/>
    <input type="checkbox" name="choose" id="checkbox4"/><label for="checkbox4">  4</label><br/>
    <input type="checkbox" name="choose" id="checkbox5"/><label for="checkbox5">  5</label><br/>
    <input type="checkbox" name="choose" id="checkbox6"/><label for="checkbox6">  6</label><br/>
    <a href="javascript:;" id="btn2">  </a>
</body>
</html>
<script type="text/javascript" src="/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(function(){
    var checkboxes = $('input[name=choose]');
    var btn = $('#btn');
    var btn2 = $('#btn2');
    
    btn.click(function(){
        checkboxes.attr('checked',this.checked);
    });
    checkboxes.click(function(){
        var flag = true;
        checkboxes.each(function(){
            if(!this.checked) flag = false;
        });
        btn.attr('checked',flag);
    });
    btn2.click(function(){
        var flag = true;
        checkboxes.each(function(){
            this.checked = !this.checked;
            if(!this.checked) flag = false;
        });
        btn.attr('checked',flag);
    });
});
</script>

 
둘.JavaScript의 전체 선택 기능:
 
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>checkbox</title>
</head>
<body>
    <input type="checkbox" name="btn" id="btn"/><label for="btn">  </label><br/>
    <input type="checkbox" name="choose" id="checkbox1"/><label for="checkbox1">  1</label><br/>
    <input type="checkbox" name="choose" id="checkbox2"/><label for="checkbox2">  2</label><br/>
    <input type="checkbox" name="choose" id="checkbox3"/><label for="checkbox3">  3</label><br/>
    <input type="checkbox" name="choose" id="checkbox4"/><label for="checkbox4">  4</label><br/>
    <input type="checkbox" name="choose" id="checkbox5"/><label for="checkbox5">  5</label><br/>
    <input type="checkbox" name="choose" id="checkbox6"/><label for="checkbox6">  6</label><br/>
    <a href="javascript:;" id="btn2">  </a>
</body>
</html>
<script type="text/javascript">
window.onload = function(){
    var oBtn = document.getElementById('btn');
    var oBtn2 = document.getElementById('btn2');
    var oInput = document.getElementsByName('choose');
    
    for(var i = 0;i < oInput.length; i++){
        oInput[i].onclick = function(){
            this.checked ? allCheck() : oBtn.checked = false;
        };
    };
    
    //  
    oBtn.onclick = function(){
        for(var i = 0;i < oInput.length; i++){
            oBtn.checked ? oInput[i].checked = true : oInput[i].checked = false;
        };
    };
    
    //  
    oBtn2.onclick = function(){
        for(var i = 0;i < oInput.length; i++){
            oInput[i].checked = !oInput[i].checked;
        };
        allCheck();
    };
};

//      
function allCheck(){
    var oBtn = document.getElementById('btn');
    var oInput = document.getElementsByName('choose');
    var allCheck = true;
    for(var i = 0;i < oInput.length; i++){
        if(!(oInput[i].checked)) allCheck = false;
    };
    allCheck ? oBtn.checked = true : oBtn.checked = false;
};
</script>

좋은 웹페이지 즐겨찾기