js 表格添加|删除一行交互

一、需求

二、实现

<div style="margin-bottom: 55px">
                    <form action="" method="post"  enctype="multipart/form-data" id="reportForm" name="sjf" style="margin-left: 25px;margin-bottom: 50px;">
                    <table id="tableContent">
                        <tbody>
                        {foreach $list ke=>va}
                        <tr id="line-1" class="layui-form layui-row layui-col-space16 tr-line">
                            <td class="layui-form">
                                 <select name="selectkey[]" class="tr-select" style="width:186px;display:inline" oninput="handleSelectChange(event)">
                                     <option value="" >请选择</option>
                                     {foreach $selectList k=>v}
                                     <option value="{$v['key']}"  {if $ke == $v['key']} selected="selected" {/if}  >{$k}</option>
                                     {/foreach}
                                 </select>
                            </td>
                            <td class="td_Item">
                                <input type="text" name="selectval[]" class="stepName" autocomplete="off" value="{$va}" oninput="handleInputChange(event)">
                            </td>
                            <td class="td_Oper" style="cursor: pointer;"><i class="layui-icon layui-icon-close" onclick="remove_line(this);"></i>&nbsp;</td>
                            <td class="td_Oper" style="cursor: pointer;"><i class="layui-icon layui-icon-addition" onclick="add_line(this);"></i>&nbsp;</td>
                        </tr>
                        {/foreach}
                        </tbody>
                    </table>
                        <input type="hidden" name="immongoid" class="stepName" autocomplete="off" value="{$val['MongoID']}">
                    <script type="text/javascript">

                        //添加新记录
                        function add_line(index) {
                            //克隆上一行
                            $("#tableContent tbody tr:last-child").clone().appendTo("#tableContent tbody");
                            //将行尾克隆的值清空
                            $("#tableContent tbody tr:last-child").find(":input").val('');
                            $("#tableContent tbody tr:last-child").find("input").css('border-color','#468')
                        }

                        //删除选择记录
                        function remove_line(index) {
                            var keyindex = $("#tableContent tbody tr").index();
                            if (keyindex > 0) {
                                console.log($(index).parent().parent())
                                $(index).parent().parent().remove();
                            } else {
                                layer.msg('第一行不能删除')
                                return false;
                            }
                        }
                        function closeFun(){
                            $('.stepName').each(function(){
                                $(this).css('border-color','#468')
                            })
                            $('.tr-select').each(function(){
                                $(this).css('border-color','#468')
                            })
                        }
                        function handleInputChange(e){
                            $('.stepName').each(function(){
                                if($(this)[0] == e.target){
                                    $(this).css('border-color','#468')
                                }
                            })
                        }
                        function handleSelectChange(e){
                            $('.tr-select').each(function(){
                                if($(this)[0] == e.target){
                                    $(this).css('border-color','#468')
                                }
                            })
                        }
						
						# 表单验证并保存,一行中有未填写内容时input框标红并阻止提交
                        function submitClick(inp){
                            let isCheck = true;
                            $('.tr-line').each(function(index,key){
                                if($(this).find('select')){
                                    if($(this).find('select option:selected').text() == '请选择'){
                                        $(this).find('select').css('border-color','red');
                                        isCheck = false
                                    }else{
                                        $(this).css('border-color','#468')
                                    }
                                    if($(this).find('input').val() == ''){
                                        $(this).find('input').css('border-color','red');
                                        isCheck = false
                                    }else{
                                        $(this).css('border-color','#468')
                                    }
                                }
                            })
                            if(!isCheck){
                                return
                            }
                            $.ajax({
                                url:'/ajax/test',
                                data:$("#reportForm").serialize(),
                                async:false,
                                type:'post',
                                dataType:'json',
                                success:function(data){
                                    layer.msg('保存成功')
                                }
                            });
                        }
                    </script>

                    </form>
                    <div class="want_box-bottom1" style="margin-left: 32px;">
                        <span><input type="submit" value="保存" onclick="submitClick(this)" class="gray-but"></span>
                    </div>
                    {/if}
                </div>

相关推荐

  1. 添加表格MFC PDF

    2024-06-07 02:14:04       38 阅读
  2. QWebEngineView与js交互

    2024-06-07 02:14:04       58 阅读
  3. 如何在three.js添加一个button

    2024-06-07 02:14:04       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 02:14:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 02:14:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 02:14:04       82 阅读
  4. Python语言-面向对象

    2024-06-07 02:14:04       91 阅读

热门阅读

  1. Kotlin 特色 sealed 关键字

    2024-06-07 02:14:04       29 阅读
  2. Kotlin 中,扩展函数(Extension Functions)

    2024-06-07 02:14:04       30 阅读
  3. docker安装mysql8和mysql5.7

    2024-06-07 02:14:04       33 阅读
  4. 问题:对象流仅读取一个对象

    2024-06-07 02:14:04       25 阅读
  5. Puppeteer用途

    2024-06-07 02:14:04       30 阅读
  6. perl: URI::rtsp 是用来处理RTSP协议的的URI的模块。

    2024-06-07 02:14:04       27 阅读
  7. js将元素滚动到可见区域

    2024-06-07 02:14:04       31 阅读
  8. 中介子方程

    2024-06-07 02:14:04       31 阅读
  9. LE Audio音频广播新功能Auracast介绍

    2024-06-07 02:14:04       27 阅读
  10. Git | SSH 密钥连接到 GitHub

    2024-06-07 02:14:04       32 阅读
  11. lsof 命令

    2024-06-07 02:14:04       30 阅读