zoukankan      html  css  js  c++  java
  • 用js判断一个复选框是否被选中

    <html> <head>
            <title>
                复选框全选、全不选、反选、必选一个
            </title>
            <meta http-equiv="content-type" content="text/html;charset=GBK"/>
            <script language="javascript">
                //表单验证
                function check(){               
                    var ids = document.getElementsByName("ids");               
                    var flag = false ;               
                    for(var i=0;i<ids.length;i++){
                        if(ids[i].checked){
                            flag = true ;
                            break ;
                        }
                    }
                    if(!flag){
                        alert("请最少选择一项!");
                        return false ;
                    }
                }
                //全选
                function iselect(){ //其中函数字不能为select 其为JS保留字
                    var ids = document.getElementsByName("ids");
                    var all = document.getElementByIdx_x_x("all");               
                    for(var i=0;i<ids.length;i++){
                        ids[i].checked=all.checked;
                    }
                }
                //全选
                function selectAll(){
                    var ids = document.getElementsByName("ids");                           
                    for(var i=0;i<ids.length;i++){
                        ids[i].checked=true;
                    }
                }
                //全不
                function selectNone(){
                    var ids = document.getElementsByName("ids");                           
                    for(var i=0;i<ids.length;i++){
                        ids[i].checked=false;
                    }
                }
                //反选
                function selectInvert(){
                    var ids = document.getElementsByName("ids");                           
                    for(var i=0;i<ids.length;i++){
                        if(ids[i].checked)
                            ids[i].checked=false ;
                        else
                            ids[i].checked=true ;
                    }
                }
            </script>
        </head>
        <body>
            <form action="#" onsubmit="return check();">
            <table border="0">
                <tr><td><input type="checkbox" id="all" onclick="iselect();"></td></tr>
                <tr><td><input type="checkbox" name="ids">音乐</td></tr>
                <tr><td><input type="checkbox" name="ids">短信</td></tr>
                <tr><td><input type="checkbox" name="ids">图片</td></tr>
                <tr><td><input type="checkbox" name="ids">电子书</td></tr>
                <tr><td><input type="checkbox" name="ids">铃声</td></tr>
                <tr><td><input type="checkbox" name="ids">漫画</td></tr>
                <tr><td><input type="checkbox" name="ids">游戏</td></tr>
                <tr><td><input type="checkbox" name="ids">杂志</td></tr>
                <tr><td><input type="checkbox" name="ids">新闻</td></tr>
                <tr>
                    <td>
                        <a href="#" onclick="selectAll();">全选</a>&nbsp;
                        <a href="#" onclick="selectNone();">全不</a>&nbsp;
                        <a href="#" onclick="selectInvert();">反选</a>
                    </td>
                </tr>
                <tr><td><input type="submit" value="提交"></td></tr>
            </table>
            </form>
        </body>
    </html>

  • 相关阅读:
    numpy排序函数:sort、argsort、lexsort、partition、sorted
    转载一份分类、回归、排序的评价指标
    python类的全面介绍
    好文推荐:转载一篇别人kaggle的经验分享
    实现ls -l
    C命令行参数
    C语言调用汇编
    汇编调用C程序
    linux 进程通信 :流套接字
    linux进程通信:消息队列
  • 原文地址:https://www.cnblogs.com/wangrong/p/6265363.html
Copyright © 2011-2022 走看看