如何在下拉列表中显示所选项目? [英] How to display selected item at drop down list?
问题描述
我目前正在使用带下拉列表的编辑表单。我有一个编辑表单,显示数据库中的当前项目。但是我不知道我的下拉菜单如何显示与下拉列表匹配的数据库中的当前值。我知道我的解释有些混乱,因此下面的图片将显示更好的解释。
I am currently working on a edit form with drop down list. I have a edit form that display current item from database. But I have no idea how my drop down menu to display current value from database that match drop down list. I know my explanation a bit confuse therefore I picture below will show better explanation.
这是我的显示下拉菜单的编辑表单之一。我想要的是通过此下拉菜单视图显示数据库值。
This is one of my edit form that showing drop down menu. What I want is display database value with this view of drop down menu.
示例:
我的数据库值为待处理
。然后它将在此下拉菜单视图中显示待处理
。当我单击下拉菜单时,它将显示如下图所示:
My database value is Pending
. Then it will show Pending
inside this view of drop down menu. And when I click on the drop down menu, it will show me like below picture :
这就是我想要的。下面是我尝试的代码。
this is what I am looking for. And below is the code that I tried.
<div class="floatleft">
<select name="select2" class="select-form-standard" >
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>
</div>
但是我只知道如何设计一个下拉菜单,却不知道如何工作。我正在使用smarty框架。有人可以给我一些解决方案吗?提前致谢。
But I just know how to design a drop down menu and do not have idea how to work like this. I am working with smarty framework. Can someone give me some solution to work on it? Thanks in advanced.
推荐答案
为必须选择的选项添加 selected
属性。假设 $ value = 0
Add selected
attribute for the option which must be selected. Assume $value = 0
<?php $value = 0; ?>
<select name="select2" class="select-form-standard" >
<?php foreach($options as $id => $option) { ?>
<option value="<?php echo $id ?>"<?php
if($id == $value) {
echo " selected";
} ?>><?php echo $option ?></option>
<?php } ?>
</select>
这篇关于如何在下拉列表中显示所选项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!