zoukankan      html  css  js  c++  java
  • 获取checkbox选中项的值,全选及清空所有选中项

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>

        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.7.2/jquery.min.js"></script>

    </head>
    <body>

    <form action="#" method="post">
        <input type="checkbox" id="select"/> 全选<br/>
        <input type="checkbox" value="1" name="items"><br/>
        <input type="checkbox" value="2" name="items"><br/>
        <input type="checkbox" value="3" name="items"> <br/>
        <input type="checkbox" value="4" name="items"> <br/>
        <input type="checkbox" value="5" name="items"> <br/>
        <input type="checkbox" value="6" name="items"> <br/>
        <input type="checkbox" value="7" name="items"> <br/>
        <input type="checkbox" value="8" name="items"> <br/>
        <input type="checkbox" value="9" name="items"> <br/>
        <input type="checkbox" value="10" name="items"> <br/>
        <input type="checkbox" value="11" name="items"> <br/>

        <input type="submit" id="submit" value="提交">
    </form>

    <script>

    $(function() {
        $("#select").click(function() {
            if ($(this).attr("checked")) {
                    $("input[name=items]").each(function() {
                        $(this).attr("checked", true);
                    });
            } else {
                $("input[name=items]").each(function() {
                    $(this).attr("checked", false);
                });
            }
        });
        //得到选中的值,ajax操作使用
        $("#submit").click(function() {
            var text="";
            $("input[name=items]").each(function() {
                if ($(this).attr("checked")) {
                    text += ","+$(this).val();
                }
            });
            alert(text);
        });
    });
        
    </script>

    </body>
    </html>

  • 相关阅读:
    Symfony2 学习笔记之报错
    Symfony2学习笔记之数据校验
    Symfony2学习笔记之HTTP Cache
    Symfony2学习笔记之表单
    Symfony2 学习笔记之插件格式
    Symfony2学习笔记之数据库操作
    Symfony2 学习笔记之内部构件
    Symfony2 学习笔记之模板使用
    让VIEWSTATE从页面中完全消失(小技巧)
    打包bat等文件成exe,双击运行不显示dos窗口,exe不报毒
  • 原文地址:https://www.cnblogs.com/muhy/p/13685941.html
Copyright © 2011-2022 走看看