checkbox 操作【全选 获取选中的值】

17 篇文章 0 订阅
7 篇文章 0 订阅
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>

<input type="checkbox" onclick="checkAll(this)">一行全选<br><br>
<input type="button"   onclick="huoqu(this)" value="获取选中的值">
<div id="box">
    <input value="11" name="test" type="checkbox"><br>
    <input value="12" name="test" type="checkbox"><br>
    <input value="13" name="test" type="checkbox"><br>
    <input value="14" name="test" type="checkbox"><br>
    <input value="15" name="test" type="checkbox"><br>
    <input value="16" name="test" type="checkbox"><br>
    <input value="17" name="test" type="checkbox"><br>
    <input value="18" name="test" type="checkbox"><br>
    <input value="19" name="test" type="checkbox"><br>
</div>

</body>
</html>
<script type="text/javascript">

    /**
     * 全选
     */
    function checkAll(obj){
        $('#box :checkbox').prop('checked',$(obj).prop('checked'));
    }

    /**
    * 获取复选框选中的值
    * */
    function huoqu(s) {

        // 数组返回格式
        var chk_value = [];
        $('input[name="test"]:checked').each(function () {
            chk_value.push($(this).val());
        });
        //["11", "12", "13", "14"]


        // 返回字符串 以,号分割  并去掉最后一个,
        var str='';
        $('input[name="test"]:checked').each(function () {
            str+=$(this).val()+',';
        });
        var res_str = str.substr(0,str.length-1);
        //11,12,13,14
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值