1. 程式人生 > >javascript+html+css簡單的實現複選框的全選與單選

javascript+html+css簡單的實現複選框的全選與單選

<!DOCTYPE html>
<html>
<head>
    <title>html+css+js簡單實現複選框全選</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <style type="text/css">

    </style>
    <script type="text/javascript">
        function quanxuan()
        {
        <!--迴圈對各單選框的操作-->
for(var i=1;i<=3;i++) { var cbox_id="cadd"+i; <!--document獲取cbbox_id--> var cbox=document.getElementById(cbox_id); //alert(cbox.value); <!--向單選框進行全選和非全選的if,ele判定--> if(document.getElementById("check1").checked) cbox
.checked=true; else cbox.checked=false; } } </script> </head> <body> <form id="form1"> <table> <tr><td><input type="checkbox" id="check1" onclick="quanxuan()" />全選</td> <td>你喜歡吃哪種水果?</
td><td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td><input type="checkbox" id="cadd1" value="1" />1</td> <td>香蕉</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td><input type="checkbox" id="cadd2" value="2" />2</td> <td>蘋果</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td><input type="checkbox" id="cadd3" value="3" />3</td> <td>梨子</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </table> </form> </body> </html>