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>

  • 相关阅读:
    [转帖]一名Delphi程序员的开发习惯(非技术问题)
    MsSQL通用数据库创建程序(源码+Demo+Exe)
    现有 Delphi 项目迁移到 Tiburon 中的注意事项[转]
    PHP的WEB服务编程工具NuSoap介绍[转]
    进程防杀Delphi版(DLL部分)
    PHP的WEB服务编程工具NuSoap介绍[转]
    现有 Delphi 项目迁移到 Tiburon 中的注意事项[转]
    用 PHP 读取和编写 XML DOM[转]
    进程防杀Delphi版(DLL部分)
    关系数据库设计
  • 原文地址:https://www.cnblogs.com/td960505/p/6123510.html
Copyright © 2011-2022 走看看