douxuanou2787 2011-03-01 01:50
浏览 65
已采纳

PHP CodeIgniter - 使用foreach下拉列表不显示“已选择”值

I am generating options for my dropdown using

// view.php
foreach ($plants as $row):
    $options[$row->plant_id] = $row->plant_name;
endforeach;

and then lower in the HTML part of view.php

//view.php
$js = 'onChange = "plantDateDelete(\'/size/get_dates_for_plant/\'+this.value);"';
echo form_dropdown('plant_id', $options, 'Select', $js);

The dropdown show options OK, but it does NOT show 'Select' as the "selected"/default value. It shows up with the first option of the array instead.

The HTML source also shows 'Select' in form_dropdown was ignored.

I really need this dropdown to show up with 'Select' as default so as to force the user to activate the onChange function.

Any idea what is going on here or how to solve this issue?

  • 写回答

1条回答 默认 最新

  • duande9301 2011-03-01 01:53
    关注

    You need to have the default option in your $options array...

    So before your foreach, do:

    $options = array('Select');
    

    I should add, this will have the value of 0 in the dropdown, but as its the first element in the array it will be selected by default, unless another option is passed as the default value.

    If you wanted to explicitly set this value as the default you would pass 0 as the default argument.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧