drz49609 2018-07-10 07:40
浏览 68
已采纳

如何在ci中使用动态添加和删除时破坏textarea值?

this is my controller:

public function detail()
{
    if($this->input->post('submit'))
    {
        $a = $this->input->post('tutorial_overview');
        $tutorial_overview = implode(",",(array) $a);
        $data = array(
                        'tutorial_overview' => $tutorial_overview
                    );

        print_r($data)
    }
}

view: detail.php

<script>
    $(document).ready(function(){
        $("#btnAdd").click(function() {
            $("#add_ck").append('<div class="con"><label class="col-sm-3 control-label no-padding-right" for="form-field-1">Sub Heading Overview</label><div class="col-sm-7"><textarea id="overview" name="overview" class="col-xs-12"></textarea></div>' + '<input type="button" class="btnRemove" value="Remove"/></div>');
        });
        $('body').on('click','.btnRemove',function() {
            $(this).parent('div.con').remove()
        });
    });
</script>

<div class="form-group" id="add_ck">
    <label class="col-sm-3 control-label no-padding-right" for="form-field-1">Sub Heading Overview</label>
    <div class="col-sm-7">
        <textarea id="overview" name="overview" class="col-xs-12"></textarea>
    </div>
    <div class="col-sm-2">
        <input id="btnAdd" type="button" value="Add" />
    </div>
</div>

In this code I have a textarea where I have used add and remove more textarea through jquery. Now the problem is that when I am using implode function to seperate textarea value with comma (,) but now what happen when I click on add button and write something inside the text box and then print value it show me last textarea value implode fuction are not working. So, how can I fix this issue ?

Thank You

  • 写回答

1条回答 默认 最新

  • dtvam48220 2018-07-10 07:52
    关注

    Hope this will help You :

    Replace textarea name attr name="overview" with this name="overview[]", both in form and js code

    And your controller method detail should be like this :

    public function detail()
    {
        if($this->input->post('submit'))
        {
            $a = $this->input->post('tutorial_overview');
    
            if ( ! empty($a))
            {
               foreach($a as $key => $item)
               {
                  $data[$key]['tutorial_overview'] = $item;
               }
             }
            print_r($data)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试