dongshen7561 2015-07-24 07:50
浏览 35
已采纳

获取图像预览?

I want to add the image upload function and before uploading the file it should show the preview. My code works perfectly fine for the first part as displayed in image posted below but cannot make out why it does not work for the rest of the part. I have added image upload function at the end of each block but it only shows the preview for the first block and not for the rest. enter image description here

Here is the code for the same.

while($row = mysqli_fetch_array($result)){

<form enctype="multipart/form-data" id="form" action="" method="post">
        <input type="file"  id="image" name="img" onChange="readURL(this);"/>
        <img id="blah" src="#" alt="your image" /><br/><br/>
        <input type="button" value="upload" onclick="javascript:uploadInage();" />
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>

function readURL(input) {

        if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah')
                .attr('src', e.target.result)
                .width(300)
                .height(340);
        };

        reader.readAsDataURL(input.files[0]);
    }


  }
</script>
}

This is just a part of all the PHP I have.

Can you please help me add preview feature to all the block.

  • 写回答

1条回答 默认 最新

  • dsuikgi9199 2015-07-24 08:14
    关注
    <script>
    
    function readURL(input) {      
            if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                //$('#blah').attr('src', e.target.result).width(300).height(340);
                 $(input).next('img').attr('src', e.target.result).width(300).height(340);
            };
            reader.readAsDataURL(input.files[0]);
        }
    
      }
    </script>
    

    THe commented line was used before. Changed it to the next line and works perfectly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度