[英]Setting selected option in laravel form
我需要像这样的 html 给出选定的值:
<select name="myselect" id="myselect">
<option value="1">Item 1</option>
<option value="2" selected='selected'>Item 2</option>
我怎样才能用 Laravel 形式实现这一点?
每个人都在谈论你去使用{!! Form::select() !!}
{!! 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()
返回先前选择的值。
你可以这样做。
<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>
使用这个包并检查文档:
https://laravelcollective.com/docs/5.2/html#drop-down-lists
形成你的 html ,你需要使用这个标记
{!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); !!}
如果选择框中的选项很少,另一种普通的简单方法很好
<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>
以 Laravel 形式设置 selected 选项非常简单:
{{ 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>
@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
<?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>
为了回应这里的其他一些答案,我刚刚在 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
我还必须编写 require 依赖项,以便我可以在我的项目中使用它
composer require "laravelcollective/html":"^5"
最后我更改了我的config/app.php
并在$aliases
数组中添加了以下内容
'Form' => Collective\Html\FormFacade::class,
如果上述任何一项停止工作,应咨询https://laravelcollective.com/docs/master/html 。
试试这个
<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>
如果您的模型之间有Eloquent 关系,您可以执行以下操作:
@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
只需简单地粘贴此代码,您将获得所需的输出。
{{ Form::select ('myselect', ['1' => 'Item 1', '2' => 'Item 2'], 2 , ['id' =>'myselect']) }}` `
您也可以针对有限的选项尝试此操作:
<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>
如果有人还在这里,这是我的简单版本。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.