在think PHP模板文件中使用JQuery动态添加文本输入框,后端think PHP

前端:html、js、jquery,后端:php、thinkPHP 

在前端网页中动态添加输入框,后端整理数据

1、前端HTML部分:

<table class="layui-table">
          <thead>
            <tr>
              <th>项目</th>
              <th>名称</th>
              <th>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-primary" style="margin-right: 0;font-size:12px;" id="add_jscs_tr">添加</button>
              </th>
            </tr>
          </thead>
          <tbody id="jishucanshu_inputs">
            {notempty name="$data.jishucanshu"}
              {volist name="$data.jishucanshu" id='jscs1'}
              <tr class="del operate">
                <td><input type="text" name="jscs_one[]" {notempty name="$jscs1[0]"}value="{$jscs1[0]}"{/notempty}></td>
                <td><textarea id="symptomTxt" oninput="autoTextAreaHeight(this)" name="jscs_two[]">{notempty name="$jscs1[1]"}{$jscs1[1]}{/notempty}</textarea></td>
                <td>
                  <button type="button" class="layui-btn layui-btn-xs layui-btn-primary delete" style="margin-right: 0;font-size:12px;">删除</button>
                </td>
              </tr>
              {/volist}
            {/notempty}
          </tbody>
        </table>

2、前端js-jQuery部分

//添加输入框
    $('#add_jscs_tr').click(function() {
        var html='<tr class="del operate"><td><input type="text" name="jscs_one[]"></td>'+
                  '<td><textarea id="symptomTxt" oninput="autoTextAreaHeight(this)" name="jscs_two[]"></textarea></td>'+
                  '<td><button type="button" class="layui-btn layui-btn-xs layui-btn-primary delete" style="margin-right: 0;font-size:12px;">删除</button></td></tr>';
        $('#jishucanshu_inputs').append(html);
        //动态添加按钮,点击删除按钮,删除当前的元素
        $("body").on("click",".delete",function(){
          $(this).parent('td').parent('.operate').remove();
        });
        $(function () {
            var ele = document.getElementById("symptomTxt");
            autoTextAreaHeight(ele);
          });
      });
//添加按钮,点击删除按钮,删除当前的元素
      $("body").on("click",".delete",function(){
        $(this).parent('td').parent('.operate').remove();
      });

后端thinkphp-php代码:

/**
     * 将两个数组的每个元素以下标为准成对放入一维数组中
     * 输入:arr1[item][item][item];arr2[item][item][item]
     * 输出:[[item0,itme1][itme0,item1][itme0,item1]]
     */
    public function arr_two_to_one($arr1,$arr2)
    {
        if((!empty($arr1)&&!empty($arr2))
            &&(is_array($arr1)&&is_array($arr2))){
            $parts_arr=array();
            for ($i=0; $i <count($arr1);$i++) {
                $parts_arr[]=array($arr1[$i],$arr2[$i]);
            }
            return serialize($parts_arr);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值