0

我有这段代码来获取文件的名称并将它们添加到下拉列表中:

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

现在我想添加一个提交按钮并从同一页面的下拉列表中打印所选项目的文件路径() 。

4

1 回答 1

1
  1. <select>为您的元素添加一个 ID :

    echo "<select id='selCase' name='case'>"; 
    
  2. 插入将包含所选值的元素

    // After closedir($dh);
    echo "<div id='divOut'></div>";
    
  3. 插入一段 JavaScript:

    document.addEventListener("ready", function () {
      document.getElementById("selCase").addEventListener("change", function () {
        var val = this.options[this.selectedIndex].value;
        document.getElementById("divOut").innerHTML = val;
      });
    });
    

我建议您阅读有关 JavaScript 开发的网站/书籍/教程!

于 2012-12-16T19:10:48.833 回答