zoukankan      html  css  js  c++  java
  • JS全选checkbox

    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
    <html xmlns="http://www.111cn.net/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=gb2312" />
    <title>javascript选择当前页面所有checkbox 复选框代码</title>
    <script language="javascript">
    function selectalls(state) 
    {
        var inputs = document.getElementsByTagName("input");
            
        for(var i=0; i< inputs.length; i++)
        {
            if(inputs[i].type == "checkbox")
            {
                inputs[i].checked =state; 
            }
        }
    }
    </script>
    </head>
    <body>
        <form id="form1" name="form1" method="post" action="">
            <input type="checkbox" name="checkbox1" id="c1" />
            <input type="checkbox" name="checkbox2" id="c2" />
            <label for="checkbox"></label>
            <input type="text" name="textfield" id="textfield" />
            <input type="submit" name="button" id="button" value="按钮" onclick="javascript:selectalls(true);" />
        </form>
        <label for="textfield"></label>
        <input type="button" name="button" id="button" value="按钮" onclick="javascript:selectalls(true);" />
    
    </body>
    </html>

    前后两个按钮都可以。参考:
    http://www.cnblogs.com/star250/archive/2007/10/31/944206.html

    radio特殊一些,因为同名的radio有两个按钮,但又只能单选,所以必须根据它的value才能判断是哪一个(value不代表被选中):

    function selectalls(state) 
    {
        var inputs = document.getElementsByTagName("input");
            
        for(var i=0; i< inputs.length; i++)
        {
    //        if (i<=7) alert(inputs[i].name + "    " + inputs[i].value + "    "+ inputs[i].checked);
            if ((inputs[i].type == "radio") && (inputs[i].value=='true'))
            {
                inputs[i].checked = state; 
            }
        }
    }
    <input type="button" name="button" id="button" value="<?php echo $ini_array['index.select.all']?>" onclick="javascript:selectalls(true);" />
  • 相关阅读:
    深入理解委托、匿名方法和 Lambda 表达式
    常见SQL问题
    LeetCode题解——四数之和
    把中台说清楚
    程序员们的三高:高并发、高性能、高可用
    论文查重是怎么查的
    LeetCode题解——最长回文子串
    六百字读懂 Git(转)
    SQL中ON和WHERE的区别
    链表排序之堆排序
  • 原文地址:https://www.cnblogs.com/findumars/p/3155315.html
Copyright © 2011-2022 走看看