我有这段代码来获取文件的名称并将它们添加到下拉列表中:
<?php
$dir = "uploads";
$dh = opendir($dir);
echo "<select name=case>";
while (($file = readdir($dh)) !== false) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$name = (pathinfo($file, PATHINFO_FILENAME));
if ($file != "." && $file != ".." && $ext == "jpg")
{
echo "<option value=".$dir."/".$file .">" . $name . "</option>";
}
}
echo "</select>";
closedir($dh);
?>
现在,我想添加一个提交按钮,并从同一页面的下拉列表中打印所选项目的文件路径(值)。
答案 0 :(得分:1)
在<select>
元素中添加ID:
echo "<select id='selCase' name='case'>";
插入包含所选值的元素
// After closedir($dh);
echo "<div id='divOut'></div>";
插入一段JavaScript代码:
document.addEventListener("ready", function () {
document.getElementById("selCase").addEventListener("change", function () {
var val = this.options[this.selectedIndex].value;
document.getElementById("divOut").innerHTML = val;
});
});
但是我建议您阅读有关JavaScript开发的网站/书籍/教程!