Php 添加新选项时重复选择(使用Laravel)

Php 添加新选项时重复选择(使用Laravel),php,laravel,Php,Laravel,当我尝试为一个问题添加一个选项时,它会起作用,但总是会生成上一个添加选项的副本 以下是我一直在使用的脚本: @section('script') <script> $(document).ready(function(){ var choiceCount = 3; var num = 3; var template = function(selector, obj) { var obj = obj ? obj : ""; var

当我尝试为一个问题添加一个选项时,它会起作用,但总是会生成上一个添加选项的副本

以下是我一直在使用的脚本:

@section('script')
<script>
$(document).ready(function(){
    var choiceCount = 3;
    var num = 3;
    var template = function(selector, obj) {
        var obj = obj ? obj : "";
        var template = $(selector).html();
        $.each(obj, function(key, value) {
            template = template.replace(new RegExp('\\['+key.toUpperCase()+'\\]',"g"), value);
            // "g" makes regexp global
        });
        return template; 
    }

    $('#addChoiceBtn').click(function(){
        if(choiceCount<=5)
        {
            choiceCount+=1;
            num+=1;
            console.log(num);
            var html = template('#choice-template', {
                number : num
            });
            $('#choicesArea').append(html);
        }
        else
        {
            $('#choicesArea').append('<br/><span class="label label-danger">Choice Limit Reached</span>');
        }
    });
});
</script>@stop
@节(“脚本”)
$(文档).ready(函数(){
var choiceCount=3;
var-num=3;
变量模板=功能(选择器,obj){
var obj=obj?obj:;
var template=$(选择器).html();
$。每个(对象、功能(键、值){
template=template.replace(新的RegExp('\['+key.toUpperCase()+'\\]',“g”)值);
//“g”使regexp全球化
});
返回模板;
}
$('#addChoiceBtn')。单击(函数(){
如果(选择计数'QuestionListController@update“,”方法“=>”放置“)}
Qestion预览
{Form::text('question',$questions->questions,数组('id'=>'question-area','class'=>'Form-control','placeholder'=>'Write question here');}

{{Form::hidden('hidden',$questions_id,array('id'=>'question_id'));} @foreach($choice作为$choices) @如果($choices->flag==0) {{Form::checkbox('flag[1]','value',false);} @否则 {{Form::checkbox('flag[1]','value',true);} @恩迪夫 {{Form::text('choice[1],$choices->choice,array('class'=>'Form-control choice'));}
@endforeach
增加选择

{Form::submit('Delete',array('class'=>'btn btn primary','style'=>'float:left','id'=>'Delete'))} {Form::submit('Save Edit',array('class'=>'btn btn primary col-md-offset-9','style'=>'float:right','id'=>'Save'))} {{Form::close()}}
@停止
这是它的控制器(我将只列出方法):

公共函数更新()
{
$id=Input::get('hidden');
$question=Input::get('question');
DB::表('问题')
->其中('id','=',$id)
->更新(数组('questions'=>$question));
$questionId=$id;
$inputs=Input::all();
//回声“;
///dd(投入);
//回声“;
foreach($k=>$v的输入['choice']
{
$choice=新选项;
$choice->choice=$v;
如果(isset($inputs['flag'][$k]))
{
$choice->flag=1;
}
$choice->questions\u id=$questionId;
$choice->save();
}
return Redirect::route('questionlist.index');
}

是否在数据库或客户端(浏览器端)添加了副本?只是想弄清楚您的javascript或实际php(laravel)是否存在问题代码。它也会进入数据库…嗯…可能是php…我还在看lol…我是laravel的新手,所以…:要真正理解这里发生的事情有点困难,你能描述一下所涉及的步骤吗?我唯一能想到的是,在每次更新之后,你可能需要清除choicesArea。你正在使用ap挂起以添加每个选项,因此我假设每次单击“保存”“编辑”时,都会发送新数据以及已附加到表单中的内容。有意义吗?
@extends('layouts.home')
@section('main')
<div id="new-question" class="container col-md-12  left-col">
    {{ Form::open(array('action' => 'QuestionListController@update', 'method' => 'PUT')) }}
        <h1 class="text-center">Qestion Preview</h1>
        <div class="form-group" align="center">
            {{ Form::text('question', $questions->questions, array('id'=>'question-area', 'class'=>'form-control','placeholder'=>'Write question here')); }}
        </div>
        <br/>
            {{ Form::hidden('hidden', $questions_id, array('id'=>'question_id')); }}
        <div id="choicesArea">
       @foreach($choice as $choices)
            <div class="input-group col-md-10 col-md-offset-1">
            @if($choices->flag == 0)
                <span class="input-group-addon">
                    {{ Form::checkbox('flag[1]','value',false); }}
                </span>
            @else
                <span class="input-group-addon">
                    {{ Form::checkbox('flag[1]','value',true); }}
                </span>
            @endif
                {{ Form::text('choice[1]',$choices->choice, array('class'=>'form-control choice')); }}
            </div>
            <br/>
        @endforeach
        </div>    
        <br/>
        <button id="addChoiceBtn" type="button" class="btn btn-default col-md-offset-8">Add Choice</button>
        <br/>
        <br/>
        {{ Form::submit('Delete', array('class'=>'btn btn-primary', 'style'=>'float:left', 'id' => 'delete')) }}
        <!-- <button style:"float:left" type="button" class="btn btn-primary">Delete</button> -->
        {{ Form::submit('Save Edit', array('class'=>'btn btn-primary col-md-offset-9', 'style'=>'float:right', 'id' => 'save')) }}
        <!-- <button style:"float:right" type="button" class="btn btn-primary col-md-offset-9">Save Edit</button> -->
    {{ Form::close() }}
</div>
<script id="choice-template" type="text/html">
    <div class="input-group col-md-10 col-md-offset-1">
        <span class="input-group-addon">
            <input name="flag[[NUMBER]]" type="checkbox" value="value">
        </span>
        <input type="text" class="form-control choice"   name="choice[[NUMBER]]"/>
    </div>
    <br/>
</script>@stop
public function update()
{
    $id= Input::get('hidden'); 
    $question = Input::get('question');
        DB::table('questions')
                            ->where('id','=', $id)
                            ->update(array('questions'=>$question));
            $questionId = $id;
    $inputs = Input::all();
    //echo "<pre>";
    ///dd($inputs);
    //echo "</pre>";
    foreach($inputs['choice'] as $k => $v)
    {

        $choice = new Choices;
        $choice->choice = $v;
        if(isset($inputs['flag'][$k]))
        {
            $choice->flag = 1;
        }
        $choice->questions_id = $questionId;
        $choice->save();        
    }
        return Redirect::route('questionlist.index');
}