zoukankan      html  css  js  c++  java
  • checkbox操作

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <label for="btnq">
    <input type="checkbox" id="btnq" class="btnQ"/>全选</label>
    <button class="del">删除</button>
    <button class="queren">确认</button>
    <button class="btnHZ">获取选中的值</button>
    <div>
    <label for="userName">
    <input type="checkbox" id="userName" name="test" class="cb_fruit" value="1"/>userName
    </label>
    <label for="userPhone">
    <input type="checkbox" id="userPhone" name="test" class="cb_fruit" value="2"/>userPhone
    </label>
    <label for="userAddress">
    <input type="checkbox" id="userAddress" name="test" class="cb_fruit" value="3"/>userAddress
    </label>
    <label for="userSize">
    <input type="checkbox" id="userSize" name="test" class="cb_fruit" value="4"/>userSize
    </label>
    <label for="userColer">
    <input type="checkbox" id="userColer" name="test" class="cb_fruit" value="5"/>userColer
    </label>
    </div>
    </body>
    </html>
    <script src="js/jquery.js"></script>
    <script>
    $(function () {

    //获取选中的值
    var intName = document.getElementsByName("test");
    $(".btnHZ").click(function () {
    intVal()
    });
    function intVal(){
    var intArr = [];
    for (k in intName) {
    if (intName[k].checked) {
    intArr.push(intName[k].value)
    }
    }
    console.log(intArr)
    }
    //确认
    $(".queren").click(function () {
    if ($("input[name='test']:checked").length == 0) {
    alert("请选中信息")
    }else{
    intVal();
    }

    });
    //全选和反选
    $(".btnQ").click(function () {
    $("input[name='test']").prop("checked", this.checked);
    });
    /*挨个选完选中后全选选中,取消一个选中全选取消选中*/
    $(".cb_fruit").change(function () {
    if ($("input[name='test']:checked").length == $(".cb_fruit").length) {
    $("#btnq").prop("checked", true);
    } else {
    $("#btnq").prop("checked", false);
    }
    });

    //删除
    $(".del").click(function () {
    if ($("input[name='test']:checked").length == 0) {
    alert("请选中删除项")
    } else {
    $("div input").each(function () {
    if ($(this).is(':checked')) {
    $(this).remove();
    }
    })
    }
    });
    })
    </script>
  • 相关阅读:
    博客园
    未释放的已删除文件
    ssh连接缓慢
    剑指 Offer 38. 字符串的排列
    剑指 Offer 37. 序列化二叉树
    剑指 Offer 50. 第一个只出现一次的字符
    剑指 Offer 36. 二叉搜索树与双向链表
    剑指 Offer 35. 复杂链表的复制
    剑指 Offer 34. 二叉树中和为某一值的路径
    剑指 Offer 33. 二叉搜索树的后序遍历序列
  • 原文地址:https://www.cnblogs.com/lmh951126/p/9234404.html
Copyright © 2011-2022 走看看