創建多個問題複選框,我需要發送兩個可能的情況1或0到數據庫,但總是發送最後一個我點擊1作爲其他所有其他0.因此,對於前。如果要是點擊查看的第一個和最後一個我想發送到數據庫均爲1,另兩個未檢查0創建多個需要發送到數據庫的複選框爲1或0
我控制器
public function create($request){
foreach ($request['options'] as $key => $option){
Answer::create([
'question_id' => $this->get(),
'text' => $option,
'correct' => $request['correct'] == $key ? 1 : 0
]);
}
}
而且我認爲
@for($i = 1; $i<=4; $i++)
<div class="form-group {{ $errors->has('options.'.$i) ? ' has-error': '' }}"
id="option{{ $i }}">
<div class="checkbox col-xs-2 control-label" style="margin-top: -2px">
<label>
<input id="cc" type="checkbox" name="correct" value="{{$i}}" {{ $i==1 ? 'checked' : '' }} >
<!-- {!! Form::hidden('correct',0) !!}
{!! Form::checkbox('correct',1,false) !!} -->
</label>
</div>
<div class="col-xs-8">
<input type="text" name="options[{{ $i }}]" value="{{ old('options.'.$i) }}"
class="form-control" placeholder="@lang('general.option') {{ $i }}">
@if($errors->has('options.'.$i))
<div class="col-xs-12"></div>
<span class="help-block">
<strong>{{ $errors->first('options.'.$i) }}</strong>
</span>
@endif
</div>
添加對應於每個複選框欄隱藏的輸入字段。將默認值設置爲0,並使用java腳本更改複選框單擊時的輸入字段值。 –
我試過這樣做 $('input [type =「checkbox」]')。 ){$ (本).attr( '值', '1'); }否則{ $(本).attr( '值', '0'); } });但現在正在發送所有輸入1 – ylli
如果選中,則必須更改隱藏輸入字段的值,您正在更改複選框的值。希望你明白 –