微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在弹出窗口中使用jQuery在输入类型=“文件”中预览选定的图像?

参见英文答案 > Preview an image before it is uploaded16个答案在我的代码中,我允许用户上传图像。现在我想显示这个选择的图像作为预览在同一个弹出。我如何使用jQuery做它?

以下是我在我的弹出窗口中使用的输入类型。

HTML代码

<input type="file" name="uploadNewImage">

解决方法

Demo

HTML:

<form id="form1" runat="server">
   <input type='file' id="imgInp" />
   <img id="blah" src="#" alt="your image" />
</form>

jQuery

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

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

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

$("#imgInp").change(function(){
    readURL(this);
});

Reference

原文地址:https://www.jb51.cc/jquery/184120.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐