layui全选,单选(checkbox)

layui全选,单选(checkbox)

js监听

/**
 * 全选
 * @param val
 * @constructor
 */
layui.use(['form'], function(){
    var form = layui.form();
    //全选
    form.on('checkbox(allChoose)', function (data) {
        $("input[name='check[]']").each(function () {
            this.checked = data.elem.checked;
        });
        form.render('checkbox');
    });
    //单选
    form.on('checkbox(oneChoose)', function (data) {
        var i = 0;
        var j = 0;
        $("input[name='check[]']").each(function () {
            if( this.checked === true )
            {
                i++;
            }
            j++;
        });
        if( i == j )
        {
            $(".checkboxAll").prop("checked",true);
            form.render('checkbox');
        }else
        {
            $(".checkboxAll").removeAttr("checked");
            form.render('checkbox');
        }

    });
});

 Html

<tr>
 <th width="40" ><input type="checkbox" class="checkboxAll" lay-skin="primary"  lay-filter="allChoose" ></th>
 <th width="210">商品名称</th>
 <th width="160">SKU</th>
 <th width="70">产品类型</th>
 <th width="140">价格(元)</th>
 <th width="140">发布时间</th>
 <th>操作</th>
</tr>
<tr>
   <td><input type="checkbox" name="check[]" value="2" lay-skin="primary" lay-filter="oneChoose"></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
</tr>

相关文章

1.第一步 设置响应头 header(&#39;Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...