duanci9305 2016-11-24 15:36
浏览 128
已采纳

通过select-option更新图像的src

How can i change the src of an image through select-option? The img-src is a php variable and once something is selected I wanted it to change a select-option from another div

The dropdown

<select>
<option value="<?php echo $aColorName; ?>">Color Name</option>
<option value="<?php echo $anotherColorName; ?>">Color Name</option>
</select>

<img src="<?php echo $colorUrl.$aColorName ?>" />

I wanted the image source to update each time a new color is selected from the options

  • 写回答

2条回答 默认 最新

  • donglu7998 2016-11-24 15:48
    关注

    And a vanilla version...

    var selectBox = document.getElementById('selectBox');
        var theImg = document.getElementById('theImg');
        
        selectBox.addEventListener('change', function() {
            theImg.src = '/images/' + this.options[this.selectedIndex].value + '.jpg';
        }, false);
    <select id="selectBox">
        <option value="blue">Blue</option>
        <option value="red">Red</option>
    </select>
    
    <img id="theImg" src="/images/<?php echo $colorUrl.$aColorName ?>.jpg" />
    
    <!--
    The 'src' attribute in image below needs to point to the default image to fix your problem. So for example if your images path is: '/images/' and in your foreach loop the value of $colorUrl.$aColorName is: 'Yellow' then the above code will output thew following when the page initially loads:
    
    <img id="theImg" src="/images/Yellow.jpg" />
    -->

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符