dongyanfeng0563 2016-02-01 11:00
浏览 34
已采纳

如何使用codeigniter set_value用于下拉输入?

I want to used set_value for input where type=text and i have no problem with that.

I am confused to use dropdown value. i am fetch dropdown values from database and i could not understand where i use set_value .

My code:

<select class="form-control" name="sport_name" id="sport_name">
 <option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):
 if($item->sport_name==$mySport) 
{?>
<option value="<?php echo $item->sport_id;  ?>" selected><?php echo $item->sport_name;  ?></option>
<?php }else{?>
<option value="<?php echo $item->sport_id;  ?>"><?php echo $item->sport_name;  ?></option>
 <? } endforeach; ?>
 </select>
  • 写回答

4条回答 默认 最新

  • doudi1979 2016-02-01 21:24
    关注

    As per the docs, you would not use set_value for a select element:

    "Permits you to set the value of an input form (text input) or textarea.".

    Rather, you would use set_select.

    If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

    set_select($field[, $value = ''[, $default = FALSE]])
    

    Parameters:

    • $field (string) – Field name

    • $value (string) – Value to check for

    • $default (string) – Whether the value is also a default one

    Returns: ‘selected’ attribute or an empty string

    Return type: string

    Example:

    <select class="form-control" name="sport_name" id="sport_name">
        <option value=''>--Select Sport--</option>
        <option value="one" <?php echo set_select('sport_name', 'one'); ?> >One</option>
        <option value="two" <?php echo set_select('sport_name', 'two'); ?> >Two</option>
        <option value="three" <?php echo set_select('sport_name', 'three'); ?> >Three</option>
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改