答案 0 :(得分:2)
您希望您的HTML最终看起来像这样 - 请注意selected
之后的"Friday"
默认选择星期五:
<select name="weekday" class="right">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday" selected>Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
可以像这样使用快速而脏的PHP示例,其中要求每个选项验证它是否是当前日期的正确选项。如果是,请显示selected
否则不显示任何内容。您可以在http://phpfiddle.org/lite上试用此代码。
<html>
<body>
<select name="weekday" class="right">
<option value="Monday" <?php echo setSelected('Monday'); ?>>Monday</option>
<option value="Tuesday" <?php echo setSelected('Tuesday'); ?>>Tuesday</option>
<option value="Wednesday" <?php echo setSelected('Wednesday'); ?>>Wednesday</option>
<option value="Thursday" <?php echo setSelected('Thursday'); ?>>Thursday</option>
<option value="Friday" <?php echo setSelected('Friday'); ?>>Friday</option>
<option value="Saturday" <?php echo setSelected('Saturday'); ?>>Saturday</option>
<option value="Sunday" <?php echo setSelected('Sunday'); ?>>Sunday</option>
</select>
</body>
</html>
<?php
function setSelected($day)
{
date_default_timezone_set('America/Chicago');
return strtolower($day) === strtolower(date('l')) ? ' selected' : '';
}
?>
我更倾向于在服务器端创建select / option。同样,快速而又脏的PHP示例:
<html>
<body>
<select name="weekday" class="right">
<?php
date_default_timezone_set('America/Chicago');
$dayOfWeek = strtolower(date('l'));
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
foreach ($days as $day)
{
echo sprintf("<option value=\"%s\" %s>%s</option>\n",
$day,
$dayOfWeek == strtolower($day) ? 'selected' : '',
$day
);
}
?>
</select>
</body>
</html>