如何在下拉框中设置所选项目 [英] How do I set the selected item in a drop down box

查看:29
本文介绍了如何在下拉框中设置所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用以下类型"代码在下拉框中设置所选项目?

Is there any way to set the selected item in a drop down box using the following 'type' code?

<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>

数据库保存一个月..我想在编辑页面上允许他们选择这个月..但要预先填充他们当前的设置?

The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?

推荐答案

需要设置正确选项标签的selected属性:

You need to set the selected attribute of the correct option tag:

<option value="January" selected="selected">January</option>

您的 PHP 将如下所示:

Your PHP would look something like this:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

我通常发现创建一个值数组并遍历它以创建下拉列表更简洁.

I usually find it neater to create an array of values and loop through that to create a dropdown.

这篇关于如何在下拉框中设置所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆