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

时间:2015-01-26 11:11:27

标签: php laravel

当我尝试为问题添加选项时,它会起作用,但总是产生最后添加选项的副本。

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

@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

这是它的blade.php:

@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');
}

0 个答案:

没有答案