繁体   English   中英

从下拉菜单中选择图像后如何显示图像?

[英]How to show an image after having selected it from a drop down menu?

我有以下代码从下拉菜单中选择图像。

<select name="Image2">
    <option value="" selected="selected"></option>
  <?php 
     $dir = "../files/images/product";//your path
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        $files[] = $filename;
  echo "<option value='" . $filename . "'>".$filename."</option>";  }
    sort($files);
 ?>

</select>   

有什么办法可以预览选定的图像? 我这样尝试过:

<img src="<?php echo $filepath.$filename ?>" />

但是它什么也没显示

尝试这个

  <script>
       $('#attribute119').change(function () {
    var path=$('#filepath').val();
      $('#main').attr('src', path+'/'+ $('#attribute119 :selected').text() );
    });
    </script>

<img src="mainimage.jpg" id="main">
<select name="Image2" id="attribute119">
    <option value="" selected="selected"></option>
  <?php 
     $dir = "../files/images/product";//your path
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        $files[] = $filename;
  echo "<option value='" . $filename . "'>".$filename."</option>";  }
    sort($files);
 ?>

</select> 
<input type=hidden id="filepath" value="<?php echo $filepath ?>"/>

也许你应该试试这个..这可以帮助你..

<select name="Image2" onChange="showImage(this.value)">
    <option value="" selected="selected"></option>
  <?php 
     $dir = "../files/images/product";//your path
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        $files[] = $filename;
  echo "<option value='".$dir ."/". $filename . "'>" . $filename . "</option>";  }
    sort($files);
 ?>

</select>  

<div id="image_div"></div> 

<script type="text/javascript">
 function showImage(value)
 {
  var img = "<img src='"+value+"' />";
  document.getElementById('image_div').innerHTML = img;
 }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM