我正在尝试使用old()保持多选下拉列表的输入。无论如何要突出显示所选值
<select name="contract_id[]" class="form-control selectpicker" multiple>
@foreach($contracts as $contract)
@if( ( isset($usercontract) && $usercontract->where('id' , $contract->id) != "[]" ) ||
( count($errors) && old('contract_id[]') == $contract->id ) )
<option value="{{ $contract->id }}" selected="">{{ $contract->name }}</option>
@else
<option value="{{ $contract->id }}">{{ $contract->name }}</option>
@endif
@endforeach
</select>
答案 0 :(得分:1)
在php中找到了使用in_array()函数的解决方案。
<select name="contract_id[]" class="form-control selectpicker" multiple>
@foreach($contracts as $contract)
@if( ( isset($usercontract) && $usercontract->where('id' , $contract->id) != "[]" ) ||
( count($errors) && in_array($contract->id, old('contract_id')) == $contract->id ) )
<option value="{{ $contract->id }}" selected="">{{ $contract->name }}</option>
@else
<option value="{{ $contract->id }}">{{ $contract->name }}</option>
@endif
@endforeach
</select>