我正在使用Yii2構建應用程序.我使用Yii2提供的Html Helper生成下拉列表:
= Html::dropDownList('food', $food_id, $foodList, ['id'=>'food-select']); ?>
其中$food_id是默認選擇的選項,$foodList是一個包含表示選項值和文本的鍵值對的數組.
它工作得很好,但我需要在我的選項中添加一個html-markup(data-food =“…”).像這樣的東西:
Apple
這可能使用Html :: dropDownList()方法嗎?無論如何要做到這一點?
解決方法:
您可以使用$options數組的options參數,如下所示:
$food_list = [1 => 'Apple', 2 => 'Banana', 4 => 'Orange']; //let's assume
= Html::dropDownList('food', $food_id, $food_list, [
'id'=>'food-select',
'options' => [
1 => ['data-food'=>'apple-info'], //index must be same as the option value
2 => ['data-food'=>'banana-info'],
4 => ['data-food'=>'orange-info']
]
]);
?>
下拉列表后的輸出 –
Apple
Banana
Orange
options: array, the attributes for the select option tags. The array
keys must be valid option values, and the array values are the extra
attributes for the corresponding option tags.
標簽:php,yii2,html
來源: https://codeday.me/bug/20190527/1165031.html