zoukankan      html  css  js  c++  java
  • Jquery 为多个元素绑定同一个事件,最好用class(多个元素指定同一个class),非要用id的话使用$('[id=xxx]')的格式

    不要用id选择器,因为每个元素id都是唯一的。

    <script>
     
            $(function () {
                $('.checkall').change(function () {
                    var str = $(this).prop('checked');
                    $('.shopcheckbox,.checkall').prop('checked', str)
                });
     
    或者:
     
        $('[id=checkall]').change(function () {
                    var str = $(this).prop('checked');
                    $('.shopcheckbox,.checkall').prop('checked', str)
                    //$(this).parent().siblings('.all').children().prop('checked', str);
                });
           
         })
        </script>
    </head>

    <body>
        <div index='test' data-index="h5"></div>

        <div class="box">

            <div class="all"><input type="checkbox" id="checkall" class="checkall"> 全选商品</div>
            <ul>
                <li>
                    <input type="checkbox" class="shopcheckbox"> 商品
                </li>
                <li>
                    <input type="checkbox" class="shopcheckbox"> 商品
                </li>
                <li>
                    <input type="checkbox" class="shopcheckbox"> 商品
                </li>
                <li>
                    <input type="checkbox" class="shopcheckbox"> 商品
                </li>
                <li>
                    <input type="checkbox" class="shopcheckbox"> 商品
                </li>
            </ul>
            <div class="all"><input type="checkbox"  id="checkall" class="checkall"> 全选商品</div>
         
        </div>
  • 相关阅读:
    toj 2819 Travel
    toj 2807 Number Sort
    zoj 2818 Prairie dogs IV
    zoj 1276 Optimal Array Multiplication Sequence
    toj 2802 Tom's Game
    toj 2798 Farey Sequence
    toj 2815 Searching Problem
    toj 2806 Replace Words
    toj 2794 Bus
    css截取字符
  • 原文地址:https://www.cnblogs.com/JeffreyZhu/p/15605029.html
Copyright © 2011-2022 走看看