【问题标题】:Codeigniter php selected item from drop downCodeigniter php从下拉列表中选择的项目
【发布时间】:2015-07-21 02:38:45
【问题描述】:

我需要从我正在使用的目录中填充下拉列表:

$dir = 'public/files/';
$files = scandir ($dir);
echo form_dropdown('myid', $files);

它工作正常,但我怎样才能从菜单中获取所选项目? 我试过使用:

$selected=$this->input->post('myid');

但它不起作用。请帮忙。谢谢。

【问题讨论】:

  • 您能否在生成的下拉列表中使用 Inspect Element 并验证它们是否设置了 values 属性?

标签: php codeigniter drop-down-menu selecteditem


【解决方案1】:

试试这个..

$dir = 'public/files/';
$files = scandir ($dir);
$selected=$this->input->post('myid');

//add selected to the function
echo form_dropdown('myid', $files, $selected);

【讨论】:

    【解决方案2】:

    我不确定这是否可行,因为根据文档,scandir() 生成一个数字数组,而form_dropdown 需要一个关联数组: p>

    $options = array(
        'small'  => 'Small Shirt',
        'med'    => 'Medium Shirt',
        'large'   => 'Large Shirt',
        'xlarge' => 'Extra Large Shirt',
    );
    

    您可能需要遍历 $files 数组以将其转换为关联数组,并确保将键设置为正确的值。

    【讨论】:

      【解决方案3】:

      首先通过jQuery获取下拉菜单的值

      var selected = $('[name="myid"] option:selected')

      然后把它放在一个隐藏的文本中。获取它的 post 值。

      【讨论】:

        【解决方案4】:

        这应该可行:

        $dir = 'public/files/';
        $files = scandir ($dir);
        foreach($files as $file){
          $array_files[$file] = $file;
        }
        echo form_dropdown('myid', $array_files);  
        

        基本上,它会在之前创建一个关联数组并将其传递给下拉列表。希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-19
          • 1970-01-01
          相关资源
          最近更新 更多