下拉选择条目选择HTML表单元素 [英] Selected Entry in a Drop Down Select HTML Form Element
本文介绍了下拉选择条目选择HTML表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此下拉列表显示文件夹中的所有文件,其中一个文件将被选中使用。有没有办法显示加载页面时选择哪个文件?目前它每次都显示选择一个文件。
This drop down list, displaying all the files from a folder, one of which will be selected for use. Is there a way to show which file is selected when you load the page? At the moment it says "select a file" every time.
<select name="image" type="text" class="box" id="image" value="<?=$image;?>">
<option value='empty'>Select a file</option>
<?php
$dirname = "images/";
$images = scandir($dirname);
// This is how you sort an array, see http://php.net/sort
natsort($images);
// There's no need to use a directory handler, just loop through your $images array.
foreach ($images as $file) {
if (substr($file, -4) == ".gif") {
print "<option value='$file'>$file</option>\n"; }
}
?>
</select>
推荐答案
与Zak和NickF的答案类似,您可以使用
And similarly to Zak's and NickF's answers, you can use
selected="selected"
,如果你喜欢去购买XHTML的话。
in your option tag if you like to go for XHTML.
(在旁注中,我的新名声不允许我添加评论到答案。)
(On a side-note, my new reputation does not allow me to add comments to answers yet.)
这篇关于下拉选择条目选择HTML表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文