编辑行时下拉显示选定值 laravel
drop-down show selected value when editing a row laravel
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control">
@if ($destinationsData != [])
@foreach($destinationsData as $info)
<option value="{{$destinationsData[$index]}}">{{$info}}</option>
@endforeach
@endif
</select>
</div>
</div>
我检索了所选 $destinationData
的 $index
,当 dd($destinationsData[$index]);
我得到了我需要的结果,但是当我将它放入值中时它没有出现,如图所示以上
在构建 <select>
和 <options>
时,您需要为每个设置一个值,以便在编辑记录时可以引用。请参阅以下内容:
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control>
@foreach($destinationsData as $key => $info)
<option value="{{ $key }}">{{ $info }}</option>
@endforeach
</select>
</div>
</div>
设置value="{{ $key }}"
,在本例中,会将值设置为从0开始的索引,编辑时可以参考:
@foreach($destinationsData as $key => $info)
<option value="{{ $key }}" {{ $record->from_destination_data == $key ? 'selected' : '' }}>{{ $info }}</option>
@endforeach
只要 $record->from_destination_data
等于任何 $key
迭代,<option>
就会有 selected="selected"
,将其设置为默认选择的选项。
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control">
@if ($destinationsData != [])
@foreach($destinationsData as $info)
<option value="{{$destinationsData[$index]}}">{{$info}}</option>
@endforeach
@endif
</select>
</div>
</div>
我检索了所选 $destinationData
的 $index
,当 dd($destinationsData[$index]);
我得到了我需要的结果,但是当我将它放入值中时它没有出现,如图所示以上
在构建 <select>
和 <options>
时,您需要为每个设置一个值,以便在编辑记录时可以引用。请参阅以下内容:
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control>
@foreach($destinationsData as $key => $info)
<option value="{{ $key }}">{{ $info }}</option>
@endforeach
</select>
</div>
</div>
设置value="{{ $key }}"
,在本例中,会将值设置为从0开始的索引,编辑时可以参考:
@foreach($destinationsData as $key => $info)
<option value="{{ $key }}" {{ $record->from_destination_data == $key ? 'selected' : '' }}>{{ $info }}</option>
@endforeach
只要 $record->from_destination_data
等于任何 $key
迭代,<option>
就会有 selected="selected"
,将其设置为默认选择的选项。