Jq 遍历 全选 全不选 反选

//全选 全不选
 $('#checkAll').click(function () {
        //判断是否被选中
        var bischecked = $('#checkAll').is(':checked');
        var fruit = $('input[name="check"]');
        bischecked ? fruit.attr('checked', true) : fruit.attr('checked', false);
   });


//反选   遍历checkbox 如果当前为选中 就设置为 不选中 反之相同
    $("#tabVouchList tr").each(function () {
        if ($("td:eq(0) input[name='check']", $(this)).is(':checked')) {
            $(this).attr('checked', false);
        } else {
            $(this).attr('checked', true);
        }
    });

 

 

   //attr 可能会出现 第二次全选没反应的情况   prop 完美解决
   $("#checkall").bind("click", function () { 
         $("input[type='checkbox']").prop("checked", this.checked); 
   });

 

  

 

 

HTML table

        <table id="tabVouchList">
            <tr>
                <th>
                    <input type="checkbox" name="checkAll" />
                </th>
                <th>
                    行号
                </th>
                <th>
                    名称
                </th>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" name="check" />
                </td>
                <td>
                    行号
                </td>
                <td>
                    名称
                </td>
            </tr>
        </table>

 

posted on 2015-11-19 10:51  苏上话  阅读(892)  评论(0编辑  收藏  举报