如何在 Laravel 5 中为选择元素添加占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32582131/
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
How to add a placeholder to a select element in Laravel 5
提问by Surya Pratim Mukherjee
This is my form, I want to add a placeholder to the select element. How can I do that?
这是我的表单,我想在 select 元素中添加一个占位符。我怎样才能做到这一点?
<div class="row">
{!! Form::model(array('method' => 'post','class'=>'post-data','files' => true)) !!}
<div class="col-md-6">
<div class="form-group">
<div class='col-md-4'>
{!! Form::label('Category Name', 'Category Name',array('class' => 'form-label')) !!}
</div>
<div class='col-md-8'>
{!! Form::select('jobfornow_category_Id',$category_result,null,array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
<div class='col-md-4'>
{!! Form::label('Sub Category Name', 'Sub Category Name',array('class' => 'form-label')) !!}
</div>
<div class='col-md-8'>
{!! Form::text('jobfornow_subcategory_Name',null,array('class' => 'form-control')) !!}
</div>
</div>
<div class='col-md-4'>
{!! Form::label('Description', 'Description',array('class' => 'form-label')) !!}
</div>
<div class='col-md-8'>
{!! Form::textarea('jobfornow_subcategory_Description',null,array('class' => 'form-control')) !!}
</div>
<div class="form-group">
<div class='col-md-4'></div>
<div class='col-md-8' style="margin-top: 10px;">
{!!Form::submit('Submit',array('class' => 'btn btn-default btn-cons'))!!}
</div>
</div>
{!! Form::close() !!}
</div>
</div>
回答by RaMeSh
The HTML select element has no placeholder attribute (only input does), so even if it's in the HTML for your input, it won't do anything.
HTML select 元素没有 placeholder 属性(只有 input 有),所以即使它在您输入的 HTML 中,它也不会做任何事情。
回答by HADI
To create a dropdown list in laravel form builder, code should be like this -
要在 laravel 表单生成器中创建下拉列表,代码应该是这样的 -
In controller -
在控制器中 -
$categories = Category::select('id', 'name')->lists('name', 'id')->prepend('Select a category', '')->toArray();
if you are using Laravel 5.3 or above use pluck() instead of lists()
如果您使用的是 Laravel 5.3 或更高版本,请使用 pluck() 而不是 lists()
and in view -
并且认为——
{!! Form::select('cat_id', $categories, old('cat_id')) !!}
Tested with Laravel 5.x.
使用 Laravel 5.x 测试。
Or if you have array like -
或者如果你有这样的数组 -
$array = ['1' => 'lorem ipsum', '4' => 'Another text'];
And after passing this array to view -
并通过此数组查看后 -
{!! Form::select('cat_id', $array, old('cat_id')) !!}
There will be no placeholder. If you pass below array -
不会有占位符。如果您通过以下数组 -
$array = ['' => 'Select category' '1' => 'lorem ipsum category', '4' => 'Another category'];
or there is a collection which you want to pass in view to build select / dropdown list then
或者有一个你想要传递的集合来构建选择/下拉列表然后
$array = $collection->prepend('Select a category', '')->toArray();
You need to pass array in order to build dropdown list.
您需要传递数组才能构建下拉列表。
Note:
array_unshift
orarray_merge
will not work as expected!
注意:
array_unshift
或array_merge
不会按预期工作!
回答by aldrin27
There's no placeholder in select just a default value
没有占位符 select 只是一个默认值
{!! Form::select('jobfornow_category_Id',
$category_result = array('' => 'Please Select') + $category_result,
array('class' => 'form-control')) !!}
Or try to use array_merge:
或者尝试使用 array_merge:
{!!
Form::select(
'myselect',
array_merge(['' => 'Please Select'], $category_result),
$selected_category_result,
array(
'class' => 'form-control',
'id' => 'myselect'
)
!!}
回答by Tarus
Try it this way...
试试这个方法...
<!-- Select Region Name -->
<div class="form-group">
{!! Form::label('region', 'Region:', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::select('region', $regions,null,['required', 'class' => 'form-control','placeholder'=>'Select Region']) !!}
</div>
</div>
It worked pretty well for me, with my placeholder in place
它对我来说效果很好,我的占位符就位
回答by emmanuel
lets say your code is as below
让我们说你的代码如下
{!! Form::text('name',null['class' => 'form-control','placeholder'=>'Select Region']) !!}
just add a comma
after NULL
since a comma
is expected before [
只需添加一个comma
afterNULL
因为 acomma
预期之前[
so this is what is expected
所以这是预期的
{!! Form::text('name',null,['class' => 'form-control','placeholder'=>'Select Region']) !!}