doukong1897 2019-04-29 10:08
浏览 72
已采纳

选择图像源

Hello is search to change one preview image by select I wrote this but there is a problem ... When I change the selection the image remains the same.

<script type="text/javascript">
function numeroImage() {
  var pays = document.getElementById('idSelect').value;
  document.getElementById('image').value=pays;
  document.getElementById('imagePreview').src="flags/Flag_"+pays+".png";
}
</script>

</head>

<body>

<select id="idSelect" onchange="numeroImage()">
    <option value="france">France</option>
    <option value="belgique">Belgique</option>
    <option value="suisse">Suisse</option>
    <option value="allemagne">Allemagne</option>
</select>

<br/>
<br/>

<div id="preview">
    <img height="75" weidth="100" src="flags/Flag_france.png" id="imagePreview">
</div>


</body>

  • 写回答

2条回答 默认 最新

  • dsfdsfdsfdsf1223 2019-04-29 10:19
    关注

    Try this Jquery code:

    <html>
        <head>
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        </head>
        <body>
            <select id="idSelect">
                <option value="france">France</option>
                <option value="belgique">Belgique</option>
                <option value="suisse">Suisse</option>
                <option value="allemagne">Allemagne</option>
            </select>
            <br/>
            <br/>
            <div id="preview">
                <img height="75" weidth="100" src="flags/Flag_france.png" id="imagePreview">
            </div>
            <script type="text/javascript">
                $(document).ready(function($){
                    $(document).on("change","#idSelect",function(){
                        var image = $(this).val();
                        var image_path ="flags/Flag_"+image+".png";
                        $("#imagePreview").attr("src",image_path);
                    });
                });
            </script>
        </body>
    </html>
    

    Try this javascript code :

    <html>
        <head>
            <script type="text/javascript">
                function numeroImage() {
                  var pays = document.getElementById('idSelect').value;
                  document.getElementById('imagePreview').src="flags/Flag_"+pays+".png";;
                }
            </script>
        </head>
        <body>
            <select id="idSelect" onchange="numeroImage()">
                <option value="france">France</option>
                <option value="belgique">Belgique</option>
                <option value="suisse">Suisse</option>
                <option value="allemagne">Allemagne</option>
            </select>
            <br/>
            <br/>
            <div id="preview">
                <img height="75" weidth="100" src="flags/Flag_france.png" id="imagePreview">
            </div>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)