zoukankan      html  css  js  c++  java
  • 全选、反选、获取选中值

    <html>
    <head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function () {
    //全选或全不选
    $("#all").click(function () {
    if (this.checked) {
    $("#list :checkbox").prop("checked", true);
    } else {
    $("#list :checkbox").prop("checked", false);
    }
    });
    //全选
    $("#selectAll").click(function () {
    $("#list :checkbox,#all").prop("checked", true);
    });
    //全不选
    $("#unSelect").click(function () {
    $("#list :checkbox,#all").prop("checked", false);
    });
    //反选
    $("#reverse").click(function () {
    $("#list :checkbox").each(function () {
    $(this).prop("checked", !$(this).prop("checked"));
    });
    allchk();
    });

    //设置全选复选框
    $("#list :checkbox").click(function () {
    allchk();
    });

    //获取选中选项的值
    $("#getValue").click(function () {
    var valArr = new Array;
    $("#list :checkbox:checked").each(function (i) {
    valArr[i] = $(this).val();
    });
    var vals = valArr.join(',');
    alert(vals);
    });
    });
    function allchk() {
    var chknum = $("#list :checkbox").size(); //选项总个数
    var chk = 0;
    $("#list :checkbox").each(function () {
    if ($(this).prop("checked") == true) {
    chk++;
    }
    });
    if (chknum == chk) {//全选
    $("#all").prop("checked", true);
    } else {//不全选
    $("#all").prop("checked", false);
    }
    }

    </script>
    </head>
    <body>
    <ul id="list">
    <li><label><input type="checkbox" value="1"> 1.时间都去哪儿了</label></li>
    <li><label><input type="checkbox" value="2"> 2.海阔天空</label></li>
    <li><label><input type="checkbox" value="3"> 3.真的爱你</label></li>
    <li><label><input type="checkbox" value="4"> 4.不再犹豫</label></li>
    <li><label><input type="checkbox" value="5"> 5.光辉岁月</label></li>
    <li><label><input type="checkbox" value="6"> 6.喜欢妳</label></li>
    </ul>
    <input type="checkbox" id="all">
    <input type="button" value="全选" class="btn" id="selectAll">
    <input type="button" value="全不选" class="btn" id="unSelect">
    <input type="button" value="反选" class="btn" id="reverse">
    <input type="button" value="获得选中的所有值" class="btn" id="getValue">

    </body>
    </html>

  • 相关阅读:
    病毒侵袭持续中---hdu3065(AC自动机模板)
    病毒侵袭---hdu2896(AC自动机)
    Keywords Search---hdu2222(AC自动机 模板)
    Theme Section---hdu4763(kmp, Next数组的运用)
    Girls' research---hdu3294(回文子串manacher)
    吉哥系列故事——完美队形II---hdu4513(最长回文子串manacher)
    String Boot-thymeleaf使用(四)
    Spring Boot-properties使用(二)
    Spring Boot-springbootHelloword(一)
    redis-运维-redis单机和集群
  • 原文地址:https://www.cnblogs.com/wz9003/p/7048976.html
Copyright © 2011-2022 走看看