【问题标题】:Clear dyanamic array on each click每次点击清除动态数组
【发布时间】:2015-08-06 11:46:51
【问题描述】:

我有下面的代码可以动态显示上传的图像,它工作正常。

$(function () {
    $("#fileupload").change(function () {
        if (typeof (FileReader) != "undefined") {
            var preview = $("#preview");
            var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
            $($(this)[0].files).each(function () {
                var file = $(this);
                if (regex.test(file[0].name.toLowerCase())) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = $("<img />");
                        img.attr("style", "height: 180px; width: 180px; margin-left:10px; margin-top: 40px;");
                        img.attr("src", e.target.result);
                        img.attr("id", "imgUserDisplay");
                        preview.append(img);
                        $('#saveImage').on({
                            'click': function () {
                                var photodisplay = $("#photodisplay");
                                var photoimg = $(' <img />');
                                photoimg.attr("style", "height: 180px; width: 180px; margin-top: 40px;cursor:pointer;");
                                photoimg.attr("id", "photoUserDisplay");
                                photoimg.attr("class", "margin");
                                photoimg.attr("data-target", "#commentDiv");
                                photoimg.attr("data-toggle", "modal");
                                photoimg.attr("src", e.target.result);

                                if (!(photoimg.attr("src") == null || photoimg.attr("src") == '')) {
                                    $('#OnlyPost').attr("style", "display:none;");
                                    $('#ImagePost').removeAttr("style");
                                    $('#imgModalDisplay').attr("src", e.target.result);
                                    photodisplay.append(photoimg);
                                    $('#fileupload').val('');
                                    $("#textareaimg").val('');
                                    preview.html("");
                                } else {
                                    alert("Un-handeled Exception Caught");
                                }
                            }
                        });
                    }
                    reader.readAsDataURL(file[0]);
                } else {
                    alert(file[0].name + " is not a valid image file.");
                    preview.html("");
                    return false;
                }
            });
        } else {
            alert("This browser does not support HTML5 FileReader.");
        }
    });
});

HTML

使用这个元标记来控制缓存,但仍然没有想要的结果

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="0" />

在此代码中,上传的图像显示在不同的 div 中,效果很好。问题是该函数在所有 div 中显示用户之前上传的图像(缓存问题?),这些 div 附加在同一数组中。每次调用此特定函数时,有什么方法可以清除数组(缓存?)?我需要帮助来理解语法。如有必要,可以提供 HTML。

【问题讨论】:

  • 使用元标签防止缓存
  • 尝试添加一些带有图像名称的参数,例如 test.jpg?time() 这将从服务器而不是从缓存中加载图像
  • 有人能建议我如何清除上面 JQuery 函数中的那个数组吗?在它几乎附加到所有 div 之后?这将很有帮助:)
  • 使用 manifest.appcache 但仍然显示以前上传的图像,我认为 jquery 函数需要更改。

标签: jquery arrays


【解决方案1】:

您的元素共享相同的静态 ID

                    img.attr("id", "imgUserDisplay");

删除它或使其成为动态的。与多个元素共享同一个 ID 可能会导致无法预料的行为。

【讨论】:

  • 你能推荐一篇创建动态ID的文章吗?
  • 有很多库可以为你做这件事:例如linkimg.attr("id", _.uniqueId("imgUserDisplay"));
  • preview.append(img) 切换到img.attr("id", _.uniqueId("imgUserDisplay")); 后无法正常工作
  • 你是否包含了 lodash 库?例如&lt;script src="://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"&gt;&lt;/script&gt;
  • preview.append(img) 开始工作:),现在让我们看看//var_name.attr("id", _.uniqueId("//some_id")); 给了我预期的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多