zoukankan      html  css  js  c++  java
  • layui 动态添加 表格数据

    静态表格:

        <table class="layui-table" id="table" lay-filter="table">
                    <thead>
                        <tr>
                            <td>价格</td>
                            <td>操作</td>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" id="layui-input" class="layui-input" name="price"></td>
                            <td>
                                <a class="layui-btn layui-btn-xs add">添加</a>
                                <a class="layui-btn layui-btn-danger layui-btn-xs del">删除</a>
                            </td>
                        </tr>
                    </tbody>
                </table>

    添加操作:

        //因为动态添加的元素class属性是无效的,所以不能用$('.add').click(function(){});
        $('body').on('click', '.add', function() {
        //你要添加的html
        var html = '<tr>'+
                '<td><input type="text" id="layui-input" class="layui-input" name="price"></td>'+
                '<td>'+
                    '<a class="layui-btn layui-btn-xs add">添加</a>'+
                    '<a class="layui-btn layui-btn-danger layui-btn-xs del">删除</a>'+
                '</td>'+
            '</tr>';
        //添加到表格最后
        $(html).appendTo($('#table tbody:last'));
        form.render();
       });

    删除操作:

       $('body').on('click', '.del', function() {
        if ($('#table tbody tr').length === 1) {
            layer.msg('只有一条不允许删除。', {
                time : 2000
            });
        } else {
            //删除当前按钮所在的tr
            $(this).closest('tr').remove();
        }
       });

    效果:

  • 相关阅读:
    python-阿里镜像源-pip
    python-Web-django-图形验证
    markdown-博客编辑
    python-爬虫-史书典籍
    python-爬虫-requests
    python-Web-项目-svn和git
    python-Web-数据库-Redis
    Codeforces Round #617 (Div. 3) A~D
    Educational Codeforces Round 81 (Rated for Div. 2)
    Codeforces Round #609 (Div. 2) A到C题
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/11592566.html
Copyright © 2011-2022 走看看