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

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

问题描述

有没有办法使用以下类型代码在下拉框中设置所选项目?

  ; 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> 

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

解决方案

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

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

您的PHP看起来像这样:

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

我通常会找到更好的值来创建一个数组,并循环创建一个下拉列表。 / p>

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?

解决方案

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

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

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天全站免登陆