【发布时间】:2015-04-24 16:29:30
【问题描述】:
有没有办法使用刀片 Form::select() 将 HTML 属性添加到下拉选择中的选项?
好吧,因为我需要这样的东西:(将不同的 CSS 类添加到 option 标签)
<select id="colors" name="colors">
<option value="1" class="blue">blue</option>
<option value="2" class="red">red</option>
<option value="3" class="yellow">yellow</option>
</select>
UPDATE(CSS类名不应与文本名相同。css类来自数据库中的一个表。)
<select id="colors" name="colors">
<option value="1" class="blue">colors-blue</option>
<option value="2" class="red">something-red</option>
<option value="3" class="yellow">banana is yellow</option>
</select>
如果只是将一个 CSS 类添加到所有这些选项中,我可以简单地使用 jQuery 来实现。但我需要添加不止一个。
PS:我将类名存储在数据库的表中。
从docs,我看不到光。我也看过 API。
更新 2(提供更多代码细节)
// In my create view I have this:
{{ Form::select( 'colored_stuffs', $colorsList, null, ['id'=>'colored_stuffs'] ) }}
// The $colorsList generate an array in the ColorsController@create
public function getCreate()
{
$colorsList = $this->colors->listAll();
}
// listAll() is defined here is this repository
public function listAll()
{
$colors = $this->model->lists('name', 'id', 'color_class');
return $colors;
}
// the HTML optput of the create view it's this
<select id="colored_stuffs" name="colored_stuffs">
<option value="1">Red is used to alert something</option>
<option value="2">A banana is yellow</option>
<option value="3">Sorry no color here</option>
</select>
// But I want this
<select id="colored_stuffs" name="colored_stuffs">
<option value="1" class="red">Red is used to alert something</option>
<option value="2" class="light-yellow">A banana is yellow</option>
<option value="3" class="black">Sorry no color here</option>
</select>
【问题讨论】:
-
Laravel 中的
Form::select()方法不会做你想要的,但它可能扩展Form类来添加你想要的功能。问题是,你使用的是 Laravel 4 还是 5?