将ID添加到Dropzone.js中的预览div

ktw*_*w16 4 html javascript jquery dropzone.js

我正在尝试为Dropzone.js中上传的每个文件添加一个id属性,所以我可以稍后对其进行排序.

这是我的代码:

Dropzone.options.pictureDropzone = {
  paramName: "file",
  addRemoveLinks: true,
  init: function() {
    this.on("success", function(file, response) {
        file.serverId = response.id;
        $(file.previewTemplate).find('.dz-preview').attr('id', "document-" + file.serverId);
    });
  }
};
Run Code Online (Sandbox Code Playgroud)




这条线

$(file.previewTemplate).find('.dz-preview').attr('id', "document-" + file.serverId);
Run Code Online (Sandbox Code Playgroud)

应该添加id,但它什么都不做.也用prop()尝试了.

如果我选择不同的元素,它确实可以正常工作.例如,这适用于.dz-details

$(file.previewTemplate).find('.dz-details').attr('id', "document-" + file.serverId);
Run Code Online (Sandbox Code Playgroud)

但我似乎找不到将其添加到dz-preview元素的方法.


HTML结构看起来像这样:

<div class="dz-preview dz-processing dz-image-preview dz-success">
    <div class="dz-details"> ... </div>
    <div class="dz-progress"> ... </div>
    <div class="dz-success-mark"> ... </div>
</div>
Run Code Online (Sandbox Code Playgroud)



感谢您的帮助 :)

小智 20

我知道这是旧的,但如果有人仍在寻找答案: -

      this.on("success", function(file, response) {
          file.previewElement.id = response.id;
      });
Run Code Online (Sandbox Code Playgroud)


Lov*_*ngh 6

将 转换previewElement为 jQuery 对象并执行任何操作。

   this.on("success", function(file, response) {
      $(file.previewElement).attr("id", response.id);
  });
Run Code Online (Sandbox Code Playgroud)


小智 5

this.on("success", function(file, response) {
    file.serverId = response.id;
    $(".dz-preview:last-child").attr('id', "document-" + file.serverId);
});
Run Code Online (Sandbox Code Playgroud)

  • 如果用户删除多个文件,这将严重失败。 (13认同)