Home  >  Article  >  Web Front-end  >  How to use checkboxes in HTML pages

How to use checkboxes in HTML pages

高洛峰
高洛峰Original
2017-03-11 11:50:153116browse

Checkboxes are very common on web pages. Whether it is an e-commerce website or a platform, you will see checks wherever there is a need to choose. Next, there are two small demos I have written before, both about check boxes. The specific codes are shared on the Script Home platform for your reference.

Check boxes are very common on web pages. , whether it is an e-commerce website or a platform, you will see check boxes wherever you need to choose. Next, there are two small demos I wrote before, both about check boxes. I hope they will be helpful to everyone.

The first one is about the operation of selecting all check boxes and inverting the selection. Of course, I also added a small function in it, that is, when the check boxes of Shangpin or other things below are all selected. Then the Select All button also becomes selected; vice versa.


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            td{
                border: 1px solid black;
                text-align: center;
            }
            table{
                border: 1px solid black;
            }
            #opp{
                border-radius: 50%;
                width: 20px;
                height: 20px;
                border: 1px style #eee;
                outline-style: none;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
                <td><input id="all" type="checkbox">全选</td>
                <td width="300px">复选框全选示例</td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input class="list" type="checkbox"></td>
                <td></td>
            </tr>
            <tr>
                <td><input id="opp" type="button">反选</td>
                <td></td>
            </tr>
        </table>
        <script>
                var vll = document.getElementById("all");
                var vlist=document.getElementsByClassName("list");
                var vopp=document.getElementById("opp");
                vll.onclick=function(){ 
                    for(var i=0;i<vlist.length;i++){    
                        vlist[i].checked=vll.checked;
                    }
                }
                for( var i=0;i<vlist.length;i++){
                    vlist[i].onclick=function(){
                        if(this.checked==false){
                            vll.checked=false;  
                            }
                        else{
                            for(var i2=0;i2<vlist.length;i2++){ 
                                if(vlist[i2].checked==false){
                                    break;
                                }
                                if(i2>=vlist.length-1){
                                    vll.checked=true;   
                                }
                            }
                        }
                    }   
                }
                vopp.onclick=function(){
                    for(var i=0;i<vlist.length;i++){
                    vlist[i].checked=!vlist[i].checked;
                        if(vlist[i].checked==false){
                            vll.checked=false;
                        }
                    }
                }
        </script>
    </body>
</html>

The second one is to customize the checkbox style, that is, we can replace our checkboxes with pictures to add a cool effect; and Here I am completely using CSS3 writing method, no JavaScript is involved;


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box1{
                width: 1000px;
                height: 50px;
                position: relative;
            }
            input{
                width: 50px;
                height: 50px;
                opacity: 1;
                display: none;
            }
            input+label{
                display: block;
                width: 50px;
                height: 50px;
                background: url(img/2.png);
                background-size: 100%;
            }
            input:checked+label{
                background: url(img/1.PNG);
                background-size: 100%;
            }
        </style>
    </head>
    <body>
        <p class="box1">
            <input type="checkbox" name="" id="input1" value="" />
            <label for="input1"></label>
        </p>
        <p class="box2">
            <input type="checkbox" name="" id="input2" value="" />
            <label for="input2"></label>
        </p>
        <p class="box3">
            <input type="checkbox" name="" id="input3" value="" />
            <label for="input3"></label>
        </p>
    </body>
</html>

The above is the operation method of check boxes in HTML pages introduced by the editor. I hope it is helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the Script House website!

The above is the detailed content of How to use checkboxes in HTML pages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn