首页 > 解决方案 > 通过dropzone上传图片后,访问父div

问题描述

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

实际上我有多个dropzone,我使用每个dropzone上方的类'.abc'进行初始化,有输入我将图像名称用于上传。这是我的代码

  $('.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

标签: javascriptphpjquerydropzone

解决方案


我可以建议你为此做些小改动。移动隐藏在里面的输入类型

就在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');
    },
});

这应该可以按您的意愿工作!


推荐阅读