【问题标题】:Create select options and get the current values for month and date创建选择选项并获取月份和日期的当前值
【发布时间】:2013-09-04 02:29:33
【问题描述】:
<select name="year"><?=options(date('Y'),1900)?></select>
<select name="month"><?php echo date('m');?><?=options(1,12,'callback_month')?></select>
<select name="day"><?php echo date('d');?><?=options(1,31)?></select>  

PHP

function options($from,$to,$callback=false)
{
    $reverse=false;

    if($from>$to)
    {
        $tmp=$from;
        $from=$to;
        $to=$tmp;

        $reverse=true;
    }

    $return_string=array();
    for($i=$from;$i<=$to;$i++)
    {
        $return_string[]='<option value="'.$i.'">'.($callback?$callback($i):$i).'</option>';
    }
    if($reverse)
    {
        $return_string=array_reverse($return_string);
    }

    return join('',$return_string);
}

function callback_month($month)
{
    return date('m',mktime(0,0,0,$month,1));
}

我应该怎么做才能将相应下拉列表中的当前月份和日期作为起始值,就像年份(2013 年)一样。

【问题讨论】:

  • 您可以将另一个参数传递给 options() 函数,即当前年份/当前月份。在连接下拉选项的函数内,检查循环中的当前值是否与 paased 参数相同,在选项中添加 selected='selected'
  • 你不应该调用数组'$return_string'。为了可读性,将其称为“$options_array”或其他名称。

标签: php html select options


【解决方案1】:

尝试如下:

将新参数 type 添加到 options() 函数

<select name="year"><?=options('year' ,date('Y'),1900)?></select>
<select name="month"><?php echo date('m');?><?=options('month', 1,12,'callback_month')?></select>
<select name="day"><?php echo date('d');?><?=options('day', 1,31)?></select>  

<?php

function options($type, $from,$to,$callback=false)
{
    $reverse=false;

    switch ($type)
    {
        case "year":
            $current = date('Y');
            break;
        case "month":
            $current = date('m');
            break;
        case "day":
            $current = date('d');
            break;
    }

    if($from>$to)
    {
        $tmp=$from;
        $from=$to;
        $to=$tmp;

        $reverse=true;
    }

    $return_string=array();
    for($i=$from;$i<=$to;$i++)
    {
        $selected = ($i == $current ? " selected" : "");
        $return_string[]='<option value="'.$i.'" '.$selected.'>'.($callback?$callback($i):$i).'</option>';
    }
    if($reverse)
    {
        $return_string=array_reverse($return_string);
    }

    return join('',$return_string);
}

function callback_month($month)
{
    return date('m',mktime(0,0,0,$month,1));
}

?>

【讨论】:

    【解决方案2】:

    只需首先检索当前日期并使用 for 循环减去每年

    【讨论】:

      【解决方案3】:

      使用selected属性:

      例如

      <select>
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="vw">VW</option>
        <option value="audi" selected>Audi</option>
      </select>
      

      在这里提琴:http://jsfiddle.net/dd3FU/

      只需将其放在 if statement 中即可。

      享受吧!

      【讨论】:

      • joespina,你的意思是不是像:if option value == date(m) then attr selected = true ?
      • 取决于你的朋友。如果由我决定,我只会让它像 if(date == date now){ echo selected } else { echo something else };任何让你更舒服的东西都会更好。这里的关键元素是selected 属性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 2021-01-07
      相关资源
      最近更新 更多