我需要像这样的html:
给出选定的值<select name="myselect" id="myselect">
<option value="1">Item 1</option>
<option value="2" selected='selected'>Item 2</option>
如何通过laravel形式实现这一目标?
答案 0 :(得分:34)
每个人都在谈论你使用{!! Form::select() !!}
但是,如果您只需要使用简单的HTML ..这是另一种方法。
<select name="myselect">
@foreach ($options as $key => $value)
<option value="{{ $key }}"
@if ($key == old('myselect', $model->option))
selected="selected"
@endif
>{{ $value }}</option>
@endforeach
</select>
当您提交表单并且验证失败时,old()
函数很有用。因此,old()
返回先前选择的值。
答案 1 :(得分:9)
答案 2 :(得分:5)
您可以这样做。
<select class="form-control" name="resoureceName">
<option>Select Item</option>
@foreach ($items as $item)
<option value="{{ $item->id }}" {{ ( $item->id == $existingRecordId) ? 'selected' : '' }}> {{ $item->name }} </option>
@endforeach </select>
答案 3 :(得分:4)
使用此软件包并查看文档:
https://laravelcollective.com/docs/5.2/html#drop-down-lists
形成你的HTML,你需要使用这个标记
{!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); !!}
答案 4 :(得分:3)
以laravel形式设置所选选项非常简单:
{{ Form::select('number', [0, 1, 2], 2) }}
输出将是:
<select name="number">
<option value="0">0</option>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
</select>
答案 5 :(得分:3)
另一种普通的简单方法如果选择框
中的选项很少,这很好<select name="job_status">
<option {{old('job_status',$profile->job_status)=="unemployed"? 'selected':''}} value="unemployed">Unemployed</option>
<option {{old('job_status',$profile->job_status)=="employed"? 'selected':''}} value="employed">Employed</option>
</select>
答案 6 :(得分:2)
尝试一下
<select class="form-control" name="country_code" value="{{ old('country_code') }}">
@foreach (\App\SystemCountry::orderBy('country')->get() as $country)
<option value="{{ $country->country_code }}"
@if ($country->country_code == "LKA")
{{'selected="selected"'}}
@endif
>
{{ $country->country }}
</option>
@endforeach
</select>
答案 7 :(得分:1)
要在此处回显其他答案,我刚才在5.6中使用的代码是
{{ Form::select('status', ['Draft' => 'Draft', 'Sent' => 'Sent', 'Paid' => 'Paid'], $model->status, ['id' => 'status']) }}
为了能够使用LaravelCollective的Form Helper,我看了https://laravelcollective.com/docs/master/html#drop-down-lists
我还不得不要求作曲家也要依赖,以便可以在我的项目中使用它
composer require "laravelcollective/html":"^5"
最后,我更改了config/app.php
并在$aliases
数组中添加了以下内容
'Form' => Collective\Html\FormFacade::class,
如果上述任何方法停止工作,请咨询
答案 8 :(得分:1)
@foreach ($categories as $category)
<option value="{{$category->id}}"
@foreach ($posts->postRelateToCategory as $Postcategory)
@if ($Postcategory->id == $category->id)
{{'selected="selected"'}}
@endif
@endforeach >
{{ $category->category_name }} </option>
@endforeach
答案 9 :(得分:0)
如果您的模型之间具有雄辩的关系,则可以执行以下操作:
@foreach ($ships as $ship)
<select name="data[]" class="form-control" multiple>
@foreach ($goods_containers as $container)
<option value="{{ $container->id }}"
@if ($ship->containers->contains('container_id',$container->id ) ))
selected="selected"
@endif
>{{ $container->number}}</option>
@endforeach
</select>
@endforeach
答案 10 :(得分:0)
您也可以尝试使用以下选项:
<select class="form-control required" id="assignedRole">
<option id = "employeeRole" selected ="@if($employee->employee_role=='Employee'){'selected'}else{''} @endif">Employee</option>
<option id = "adminRole" selected ="@if($employee->employee_role=='Admin'){'selected'}else{''} @endif">Admin</option>
<option id = "employerRole" selected ="@if($employee->employee_role=='Employer'){'selected'}else{''} @endif">Employer</option>
</select>
答案 11 :(得分:0)
<?php
$items = DB::table('course')->get()->pluck('name','id');
$selectID = 3;
?>
<div class="form-group">
{{ Form::label('course_title', 'Course Title') }}
{!! Form::select('myselect', $items, $select, ['class' => 'form-control']) !!}
</div>
这显示了以下选项的相似类型:
<select name="myselect" id="myselect">
<option value="1">Computer Introduction</option>
<option value="2">Machine Learning</option>
<option value="3" selected='selected'>Python Programming</option>
<option value="4">Networking Fundamentals</option>
.
.
.
.
</select>
答案 12 :(得分:0)
如果有人还在这里,这是我的简单版本。
<select name="status" class="js-example-basic-single w-100">
<option value="" @if($brand->status == '') ? selected : null @endif disabled>Choose what to be seen on brand section</option>
<option value="TA" @if($brand->status == 'TA') ? selected : null @endif>Title Active</option>
<option value="ITA" @if($brand->status == 'ITA') ? selected : null @endif>Image Title Active</option>
</select>
答案 13 :(得分:-1)
只需粘贴此代码即可获得所需的输出。
{{ Form::select ('myselect', ['1' => 'Item 1', '2' => 'Item 2'], 2 , ['id' =>'myselect']) }}` `