如何在 smarty 3.1 中使用 {html_options} 在下拉列表中添加标题属性

How to add title attribute in drop down using {html_options} in smarty 3.1

这是我的代码。

PHP 代码

$smarty->assign('myOptions', array(
                            1800 => 'ABC',
                            9904 => 'DEF',
                            2003 => 'GHI')
                            );
$smarty->assign('mySelect', 9904);

智能代码

{html_options name=foo options=$myOptions selected=$mySelect}

期望HTML输出

<select name="foo">
 <option value="1800" title="ABC">ABC</option>
 <option value="9904" title="DEF" selected>DEF</option>
 <option value="2003" title="GHI">GHI</option> 
</select>

你不能;至少不使用默认的 html_options 函数。您可以尝试修改它或更好地创建自己的插件以允许更多参数或复杂数组,但除此之外,最好的方法是使用 foreach 在模板中生成选项列表。

另一种选择是使用 javascript 为每个 <option> 添加标题属性,以便在页面加载后选择或所有选择。