我想进行测验,并使用ajax将表单更改为下一个ID,并使用变量next_question_id,它将显示具有下一个ID的表单,我使用Response :: json更改变量值,但使用json将页面转到空白数据,而不显示带有下一个ID的表单。
我的表格:
SELECT
first_name,
Last_name,
country,
custom_field
FROM sales_table
WHERE cast( custom_field as varchar(max)) LIKE '%BMWZ4%'
我的脚本以显示下一个表单:
<div class="jumbotron" id="jumbotron{{$question->id}}"
@if($question->id != $current_question_id)
style="display: none;"
@endif
>
<p>Question#{{$question->id}}</p>
<p>{{$question->question}}</p>
{!! Form::open(['action'=>['QuizDataController@postSaveQuestionResult',$categoryquiz->id],'method'=>'post','id'=>'frm'.$question->id]) !!}
<div id="answer-radio{{$question->id}}">
<div class="btn-group" data-toggle="buttons">
<label>
<input type="radio" name="option" value="1">{{$question->option1}}
</label><br>
<label>
<input type="radio" name="option" value="2">{{$question->option2}}
</label><br>
<label>
<input type="radio" name="option" value="3">{{$question->option3}}
</label><br>
<label>
<input type="radio" name="option" value="4">{{$question->option4}}
</label><br>
</div>
</div>
{!!Form::input('hidden','question_id',$question->id)!!}
{!!Form::input('hidden','time_taken'.$question->id,null,['id'=>'time_taken'.$question->id])!!}
{!!Form::token()!!}
@if($question->id == $last_question_id)
{!!Form::submit('Last',['class'=>'btn btn-info'])!!}
@else
{!!Form::submit('Next',['class'=>'btn btn-info'])!!}
@endif
{!!Form::close()!!}
</div>
我的控制器,用于保存每个ID的答案:
$(function(){
console.log({{$question->id}});
$('#frm{!!$question->id!!}').on('submit',function(e){
e.preventDefault();
var form = $(this);
var $formAction= form.attr('action');
var $userAnswer = $('input[name=option]:checked',$('#frm{{$question->id}}')).val();
$.post($formAction,$(this).serialize(),function(data){
$('#jumbotron{{$question->id}}').hide();
$('#jumbotron'+data.next_question_id+'').show();
});
});
});
我的控制器显示测验:
public function postSaveQuestionResult($id,Request $req){
$categoryquiz = JenisQuiz::find($id);
$question = Question::find($req->get('question_id'));
if($req->get('option')!=null){
// dd($categoryquiz->duration);
$duration = $categoryquiz->duration*60;
$time_taken =((int)$req->get('time_taken'.$question->id));
$time_per_question = $duration - $time_taken;
// dd($time_per_question);
Answer::create([
'user_id'=>Auth::user()->id,
'question_id'=>$req->get('question_id'),
'jenis_quiz_id' => $id,
'user_answer' => $req->get('option'),
'question' => $question->question,
'option1' => $question->option1,
'option2' => $question->option2,
'option3' => $question->option3,
'option4' => $question->option4,
'time_taken' => $time_per_question
]);
}
$next_question_id = $categoryquiz->questions()->where('id','>',$req->get('question_id'))->min('id');
// dd($next_question_id);
// if($next_question_id != null) {
// return \Response::json(['next_question_id'=>$next_question_id]);
// }
if($next_question_id != null){
return Response::json(['next_question_id'=>$next_question_id]);
}
return redirect()->route('result',[$id]);
}
expected:显示下一个巨型机并隐藏上一个巨型机 结果:空白页并显示json数据,如下所示:
public function getStartQuiz($id){
session()->forget('next_question_id');
$categoryquiz = JenisQuiz::find($id);
// dd($categoryquiz);
$questions = $categoryquiz->questions()->get();
// dd($questions->count());
$first_question_id = $categoryquiz->questions()->min('id');
// print_r($first_question_id);
$last_question_id = $categoryquiz->questions()->max('id');
// dd($first_question_id,$last_question_id);
$duration = $categoryquiz->duration;
// dd(session('next_question_id'));
if(session('next_question_id')){
$current_question_id = session('next_question_id');
}
else{
$current_question_id = $first_question_id;
session(['next_question_id'=>$current_question_id]);
}
return view('user.pages.showquiz', compact('categoryquiz', 'questions', 'current_question_id', 'first_question_id', 'last_question_id', 'duration'));
}
成功隐藏和显示下一个问题ID一次,当我再次单击“下一步”失败时,它必须仍在测验页面上 但它停留在路线postSaveQuestionResult中,我认为问题是当它进入Response :: json(['next_question_id'=> $ next_question_id]);抱歉,我的英语不好,谢谢