下拉表单,其中包含来自变量的选定值

时间:2012-10-27 11:08:08

标签: php html

我的配置文件中存储了我的脚本的当前主题(JQUERY_THEME)如何在下拉列表中创建预选值。

$jquerytheme = JQUERY_THEME;
$themes = array("base", "black-tie", "blitzer", "cupertino", "dark-hive", "dot-luv", "eggplant", "excite-bike", "flick", "hot-sneaks", "humanity", "le-frog", "mint-choc", "overcast", "pepper-grinder", "redmond", "smoothness", "south-street", "start", "sunny", "swanky-purse", "trontastic", "ui-darkness", "ui-lightness", "vader");

echo "<p><select name=\"jquerytheme\">";
    foreach ($themes as $value) {
       echo "<option value=\"$value\">". ucfirst($value) . "</option>";
    }
echo "</select></p>";

1 个答案:

答案 0 :(得分:1)

如果您想要预先选择一个选项,则必须使用选项中的selected属性:

echo "<p><select name=\"jquerytheme\">";
    foreach ($themes as $value) {
       $selected = ($value == $jquerytheme) ? 'selected="selected"' : '';
       echo "<option $selected value=\"$value\">". ucfirst($value) . "</option>";
    }
echo "</select></p>";