如何使用 Illuminate/Html 在 Laravel 中设置禁用选择选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29771001/
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 set disabled select option in Laravel with Illuminate/Html
提问by suarsenegger
I'm starting with Laravel and I'm using Illuminate/Html for making forms.
我从 Laravel 开始,我使用 Illuminate/Html 来制作表单。
I want to add disabled attribute to the first option and I dont find the way to do it.
我想将禁用属性添加到第一个选项,但我没有找到方法。
{!! Form::open(['url' => 'shelter/pets']) !!}
<div class="form-group">
{!! Form::label('pet_type','Type:') !!}
{!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add pet', null, ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
回答by Sougata Bose
Just pass the disabled
in the options
. Try with -
只需disabled
在options
. 尝试 -
{!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control', 'disabled' => true]) !!}
You can do it manually looping through the array in php or by using jquery.
您可以手动循环遍历 php 中的数组或使用 jquery。
$('select.someclass option:first').attr('disabled', true);
回答by dotty
Looking through source, it looks like it's not possible. The <select>
element is built up at https://github.com/illuminate/html/blob/master/FormBuilder.php#L532
查看源代码,看起来这是不可能的。该<select>
元素建立在 https://github.com/illuminate/html/blob/master/FormBuilder.php#L532
The only args passed is the value, the name and the selected boolean. It looks like you have 2 solutions. Use javascript (argh), or use something like str_replace
.
唯一传递的参数是值、名称和选定的布尔值。看起来您有 2 个解决方案。使用 javascript (argh),或使用类似str_replace
.
<?php
$field = Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control']);
// find value="Select Type" and replace with value="Select Type" dialled
echo str_replace('value="Select Type"', 'value="Select Type" disabled', $field);
?>
回答by mininoz
This might not be exactly what you are looking for but it will prevent user to select the first option but still be in the list.
这可能不是您要查找的内容,但它会阻止用户选择第一个选项但仍位于列表中。
Form::select
supports A Grouped List
and you can use it this way.
Form::select
支持A Grouped List
,您可以通过这种方式使用它。
{!! Form::select('pet_type', ['Select Type' => ['dog', 'cat']], 0, ['class' => 'form-control']) !!}
More details: http://laravel.com/docs/4.2/html#drop-down-lists
回答by powerangelito
This could help you
这可以帮助你
Form::select('name_select', '', null, ['name' => '', 'id' => '', 'disabled' => 'disabled'])
回答by vanadium23
The last array is used to form attributes of html tag, so you simple pass disabled into it:
最后一个数组用于形成 html 标签的属性,因此您可以简单地将 disabled 传递给它:
{!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control', 'disabled' => 'disabled']) !!}