javascript - 通过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
解决方案
我可以建议你为此做些小改动。移动隐藏在里面的输入类型
就在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');
},
});
这应该可以按您的意愿工作!
推荐阅读
- css - 我的字体没有在浏览器中显示
- tcl - 如何使用 struct::graph(::op) 找到两个节点之间的最短路径?
- clojure - 干净地更新定义类型的类的字段
- javascript - 按键功能
- content-management-system - Netlify 管理和应用更改到多个 NetlifyCMS 站点
- r - r igraph - 根据节点的公共连接和与第三个节点的相同类型的关系创建节点之间的关系
- java - macOS:私有 JRE 应忽略全局扩展目录
- python - np.nan 加上 DataFrame 改变了 np.random.randint 行为
- tomcat - 配置 Tomcat 以记录启动时加载的所有 Jar 文件和/或类
- android - 调试 Android 应用程序:安装 apk 给出“未安装应用程序”