zoukankan      html  css  js  c++  java
  • 使用JQuery获取被选中的checkbox的value值 以及全选、反选

    以下为使用JQuery获取input checkbox被选中的值代码:
    
    <html>
        <head>
            <meta charset="gbk">
            <!-- 引入JQuery -->
            <script src="jquery-1.3.1.js" type="text/javascript"></script>
        </head>
    
        <body>
            <input type="checkbox" value="橘子" name="check">橘子1</input>
            <input type="checkbox" value="香蕉" name="check">香蕉1</input>
            <input type="checkbox" value="西瓜" name="check">西瓜1</input>
            <input type="checkbox" value="芒果" name="check">芒果1</input>
            <input type="checkbox" value="葡萄" name="check">葡萄1</input>
            
            <input type="button" value="方法1" id="b1">
            <input type="button" value="方法2" id="b2">
    
    
        </body>
        
        <script>
            //方法1
            $("#b1").click(function(){
                //$('input:checkbox:checked') 等同于 $('input[type=checkbox]:checked')
                //意思是选择被选中的checkbox
                $.each($('input:checkbox:checked'),function(){
                    window.alert("你选了:"+
                        $('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val());
                });
            });
            
            //方法2
            $("#b2").click(function(){
                $.each($('input:checkbox'),function(){
                    if(this.checked){
                        window.alert("你选了:"+
                            $('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val());
                    }
                });
            });
        </script>
    </html>

    实现全选反选 注意用:prop

        // 全选
       
        $('#allcheck').click(function(){
    
            $('input[name="check"]').prop('checked','true');
        });
    
        //反选
        $('#reversecheck').click(function(){
            $('input[name="check"]').each(function () {
                $(this).prop("checked", !$(this).prop("checked"));
            });
    
        });
    
    //----------------------------------- 第二种方法
    
                $("#all").click(function () {
                    console.log("all");
                    $("[name='checkbox']").prop("checked", true);
                })
                $("#reverse").click(function () {
                    console.log("reverse");
                    $("[name='checkbox']").each(function () {
                        if ($(this).prop("checked")) {
                            $(this).removeAttr("checked");
                        }
                        else {
                            $(this).prop("checked", true);
                        }
                    })
                }) 
  • 相关阅读:
    Win7 VSCode 在线安装Rust语言及环境配置
    Win7 VSCode 离线安装Rust语言及环境配置
    Win7崩溃程序目录
    fatal error C1047: The object or library file xxx was created with an older compiler than other objects
    Notepad++正则表达式合并多行代码为1行
    Win7 VS2019安装后创建C++工程失败解决
    关于Visual Studio中书签Bookmark的一些问题
    Fira Code,可以让不等号!=直接显示出来的字体
    免费商用字体
    Win7 64位注册32位DLL
  • 原文地址:https://www.cnblogs.com/xxaxx/p/8580092.html
Copyright © 2011-2022 走看看