2014-03-19 62 views
0

我有以下代碼從下拉菜單中選擇我的圖像。如何從下拉菜單中選擇圖像後顯示圖像?

<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 ?>" /> 

但是它並沒有顯示任何

+0

預覽如何?您向我們展示了輸出「select」的代碼。你不能在那裏有圖像。進一步解釋你打算如何使預覽機制起作用。 – Utkanos

+0

你給的$ filepath是什麼? – sunny

+0

文件路徑是在這種情況下選定文件的路徑../files/images/product – Ria

回答

0

可能是你應該嘗試這個.. 這可以幫助你..

<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> 
0

試試這個

<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 ?>"/> 
+0

它沒有工作,它做的是顯示一個實際的圖像框,但是當選擇圖像名稱前,他菜單它沒有顯示圖像。 – Ria

+0

好!現在編輯答案,檢查它是否有效。我用jquery! – sunny

+0

我很抱歉沒有快樂。 – Ria