【问题标题】:After image uploaded through dropzone, access parent div通过dropzone上传图片后,访问父div
【发布时间】:2019-09-28 06:39:22
【问题描述】:

我很困惑,我不知道如何访问父 div。让我总结一下!

实际上我有多个dropzone,我使用类'.abc'初始化 在每个 dropzone 上方都有输入,我在其中放置图像名称以进行上传。这是我的代码

  $('.kt_dropzone_1').dropzone({
           url: base_url + 'product/uploadPic', // Set the url for your upload script location
        paramName: "file", // The name that will be used to transfer the 

        maxFiles: 1,
        maxFilesize: 5, // MB
        addRemoveLinks: true,
        accept: function (file, done) {

            done();


        },
        success: function (file, response) {


            alert($(this).attr('data-abc')); // undefined
            alert($(this).parent().parent.html()); // undefined                

        },

    });

这里是 html,它在循环中,所以考虑它是多个

<input type="hidden" class="img-upload" name="old" value=""/>                       
<div class="col-md-12 pull-left">
    <div class="dropzone dropzone-default kt_dropzone_1">
        <div class="dropzone-msg dz-message needsclick">

        <h3 class="dropzone-msg-title">Drop files here or click to upload.</h3>

        <span class="dropzone-msg-desc">This is just a demo 
            dropzone. Selected files are <strong>not</strong> 
            actually uploaded.</span>
        </div>
    </div>
</div>

我尝试使用自定义属性来传递输入 id 动态仍然不起作用 我不知道如何获取当前 div 对象以访问父 html 或输入或 div

【问题讨论】:

  • 在匿名回调函数this的范围内不再引用父级 - 您可以尝试将函数绑定到父级以访问this
  • @RamRaider 怎么样?
  • 不确定这是否可行,因为我不知道thisdropzone 函数调用中指的是什么,但您可以尝试success: function (file, response) { /* do stuff */ }.bind( this ),

标签: javascript php jquery dropzone


【解决方案1】:

我可以建议您对此进行一些小改动。移动隐藏在里面的输入类型

并且就在 dropzone div 的上方。

现在要参考正确的dropzone,请尝试以下更新的代码。

$('.kt_dropzone_1').dropzone({
    var this_dropzone = $(this);
    url: base_url + 'product/uploadPic', // Set the url for your upload script location
    paramName: "file", // The name that will be used to transfer the 

    maxFiles: 1,
    maxFilesize: 5, // MB
    addRemoveLinks: true,
    accept: function (file, done) {
        done();
    },
    success: function (file, response) {
        this_dropzone.closest("div").find(".img-upload").attr('data-abe', 'value to be set');
        this_dropzone.closest("div").find(".img-upload").val('you can set anyvalue here');
    },
});

这应该可以如你所愿!

【讨论】:

  • 不,这不起作用它显示错误! { var this_dropzone = $(this); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
相关资源
最近更新 更多