下拉列表中选定的条目选择 HTML 表单元素
该下拉列表显示文件夹中的所有文件,将选择其中一个文件进行使用。 有没有办法在加载页面时显示选择了哪个文件? 目前它每次都会说“选择一个文件”。
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与 Zak 和 NickF 的答案类似,
如果您喜欢使用 XHTML,则可以在选项标签中使用。
(顺便说一句,我的新声誉还不允许我在答案中添加评论。)
And similarly to Zak's and NickF's answers, you can use
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.)
我感觉SO正在一点一点地为你写你的申请......
无论如何,
I get the feeling SO is writing your application for you bit by bit...
anyway,
在您选择的文件选项上使用“选定”标签
首先检查以查看从帖子或获取中选择了哪个文件(不清楚表单从您的帖子中采取什么操作..假设获取)
在中使用三元运算符你的循环:
use the "selected" tag on your option for the file that is selected
first check to see which file is selected from the post or get (it's unclear what action the form is taking from your post.. assuming get)
use the ternary operator in your loop: