php 选择第一个选项为空的框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24912295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:32:26  来源:igfitidea点击:

Select box with first option empty

phplaraveldrop-down-menularavel-4options

提问by JaviZu

Does anybody know how can I set in my select box the first option to empty value?

有谁知道如何在我的选择框中将第一个选项设置为空值?

I'm getting the data from my DB, and I would like to set the option by default as "Please select one option".

我正在从我的数据库获取数据,并且我想将该选项默认设置为“请选择一个选项”。

回答by darkbluesun

I found that 'default'=>'Please select'doesn't work with the HTML5 required attribute. This does work:

我发现这'default'=>'Please select'不适用于 HTML5 required 属性。这确实有效:

$listOfValues = [1 => 'Choice 1'];
Form::select('fieldname',[null=>'Please Select'] + $listOfValues);

If you don't like modern PHP syntax,

如果你不喜欢现代 PHP 语法,

$listOfValues = array(1 => 'Choice 1');
$listOfValues[null] = 'Please Select';
Form::select('fieldname', $listOfValues);

But the point is to have a label for the null value.

但关键是要有一个空值的标签。

回答by endroo

If you are using the HTML package by LaravelCollective, you do the following..

如果您使用LaravelCollectiveHTML 包,请执行以下操作。

Form::select('size', array('L' => 'Large', 'S' => 'Small'), null, ['placeholder' => 'Pick a size...']);

回答by Maurice

There are 2 methods to do this:

有两种方法可以做到这一点:

{{ Form::select('user', array('default' => 'Please select one option') + $users, 'default') }}

Or

或者

<select>
     <option selected disabled>Please select one option</option>
     @foreach($users as $user)
     <option value="{{ $user->id }}">{{ $user->name }}</option>
     @endforeach
</select>

回答by Laerte

For anyone that needs this behavior, this way is working fine:

对于任何需要这种行为的人来说,这种方式工作正常:

Controller:

控制器:

$entityArray = Entity::lists('name', 'id');
$entityArray->prepend('Select', 'Select');

View:

看法:

{!! Form::select('entity', $entityArray) !!}

回答by GRIFFIN KISIA

This worked for me on Laravel 5.4.

这在 Laravel 5.4 上对我有用。

{{ Form::select('agency', $agency, null, [
    'placeholder' => 'Please select ...',
    'class' => 'form-control'
]) }}

回答by md asif rahman

in controller

在控制器中

$data['options']=Entity::pluck('name','id')->prepend('Please Select','');

return view('your_view_blade',$data);

in view blade

在视图刀片

{!! Form::select('control_name',$options,null,['class'=>'your_class']) !!}

回答by Matthew Skaman DonDobias

100% result:

100% 结果:

In controller:

在控制器中:

$users = App\User::get()->lists('full_name', 'id')->prepend('Select user','');
return view('name of view')->with('users', $users);

In view:

鉴于:

{!! Form::select('who', $users, null, ['class' => 'form-control inline']) !!}

I′m using "laravelcollective/html":"^5.3.0" package

我正在使用 "laravelcollective/html":"^5.3.0" 包

回答by StevenW

For a Laravel 5 collection, you may need to convert the collection to array first.

对于 Laravel 5 集合,您可能需要先将集合转换为数组。

<?php
$defaultSelection = [''=>'Please Select'];
$users = $defaultSelection + $users->toArray();?> 

and apply $users as

并将 $users 应用为

{!! Form::select('user', $users); !!}

回答by sanu

In Laravel 5.1 i solved it by doing

在 Laravel 5.1 中我解决了它

$categories = [''=>''] + Category::lists('name', 'id')->toArray();
return view('products.create', compact('categories'));

Or

或者

$categories = [''=>''] + Category::lists('name', 'id')->all();
return view('products.create', compact('categories'));

回答by Yousef Altaf

In laravel 5.2

在 Laravel 5.2

This worked for me

这对我有用

{!! Form::select('user', $users, null, array('class'=>'form-control', 'placeholder' => 'Please select')) !!}

as I just add placeholderand it made the trick

正如我刚刚添加的那样placeholder,它成功了