HTML <select>默认选项

时间:2015-07-03 03:29:52

标签: php html html-select server-side

我知道之前已经提出过这个问题,但我对此有一个未解决的问题。 如何获得由serverside脚本生成的默认值? 例如: 工作日:&lt;选择姓名=&#34;工作日&#34;类=&#34;右&#34; value =&#34;&lt;%= rs(&#34; Weekday&#34;)%&gt;&#34;&gt; &lt; option value =&#34; Monday&#34;&gt; Monday&lt; / option&gt; &lt; option value =&#34; Tuesday&#34;&gt; Tuesday&lt; / option&gt; &lt; option value =&#34; Wednesday&#34;&gt; Wednesday&lt; / option&gt; &lt; option value =&#34; Thursday&#34;&gt; Thursday&lt; / option&gt; &lt; option value =&#34; Friday&#34;&gt; Friday&lt; / option&gt; &lt; option value =&#34; Saturday&#34;&gt; Saturday&lt; / option&gt; &lt; option value =&#34; Sunday&#34;&gt; Sunday&lt; / option&gt; &LT; /选择&GT; 我知道你可以使用常规元素选择属性,但是我在这里抛出一个循环。 有任何想法吗?

1 个答案:

答案 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>