【问题标题】:why the image preview can not show any image in jquery?为什么图像预览不能在 jquery 中显示任何图像?
【发布时间】:2018-03-17 09:02:28
【问题描述】:

我有一个脚本,用于在上传到数据库之前预览图像。但是,不知何故,当我测试它时,它没有预览任何图像,我不知道那里有什么问题。这是我的脚本:

$(document).ready(function(){
    $("#uploadbtn").change(function(){
        var countFiles = $(this)[0].files.length;
        var imgPath = $(this)[0].value;
        var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
        var image_holder = $("#show");
        image_holder.empty();

        if(extn == "jpg" || extn == "png" || extn == "jpeg" || extn == "gif") {
            if(typeof(FileReader) != "undefined") {
                for (var i = 0; i < countFiles; i++) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $("<img />", {
                            "src" : e.target.result,
                            "weight" : 100,
                            "height" : 100
                        }).appendTo(image_holder);
                    }
                    image_holder.show();
                    reader.readAsDataURL($(this)[0].files[i]);
                }
            } else {
                alert("tidak support FileReader");
            }
        } else {
            alert("hanya untuk gambar");
        }
    });
});

有什么问题或错误吗?

【问题讨论】:

    标签: jquery image preview


    【解决方案1】:

    您可以通过我的 JSFIDDLE 链接获得答案

    https://jsfiddle.net/yfgnz8m5/Click on link

    【讨论】:

    • 我已经在您的链接中尝试过它并且它可以工作,然后我复制它并使用我的输入 id 更改 id。但它仍然不适用于我的脚本
    猜你喜欢
    • 2014-10-26
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多