【发布时间】:2016-12-05 09:55:51
【问题描述】:
您好,我正在使用[dropzone.js](拖放文件库)
我希望用户可以单击某个按钮并添加 Dropzone 表单。 (我已经有 1 个 dropzone 表单,用户点击添加后,将显示第二个 Dropzone 表单。)
但我的问题是单击添加按钮后,dropzone 表单添加成功但无法上传文件。我应该怎么做才能让它像第一个 Dropzone 表单一样上传? (哦,如果我正常编写第二个 dropzone 表单,它可以正常工作。但如果 onclick 添加第二个 dropzone-form 则不起作用。)
这是我的代码。
<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/dropzone.js"></script>
<script>
count=2;
function add_ptg_input() //onclick to add dropzone-form function
{ if(count<50)
{
var newspan = document.createElement('span');
newspan.innerHTML = '<form id="uploadfree" class="dropzone dz-clickable" action="test2.php" method="post" style="width:400px;" > \
<div class="dropzone-previews"></div> \
<input type="text" name="free" /> \
<button type="submit">Submit data and files!</button> \
<div class="dz-default dz-message"><span>Drop files here to upload</span></div> \
</form> ';
document.getElementById('add_ptg').appendChild(newspan);
count++;
}
}
</script>
<script>
//Just a config to make dropzone can use with other form input
Dropzone.options.uploadfree = {
autoProcessQueue: false,
parallelUploads: 100,
maxFiles: 100,
init: function() {
var myDropzone = this;
this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
this.on("sendingmultiple", function() {
});
this.on("successmultiple", function(files, response) {
});
this.on("errormultiple", function(files, response) {
});
}
}
</script>
</head>
...
<body>
<a href="javascript:void(0);" onClick="add_ptg_input();" style="position:absolute; margin-left:10px;">Add</a>
<div id="add_ptg">
<form id="uploadfree" class="dropzone" action="test2.php" method="post" style="width:400px;" >
<div class="dropzone-previews"></div> <!-- this is were the previews should be shown. -->
<!-- Now setup your input fields -->
<input type="text" name="free" />
<button type="submit">Submit data and files!</button>
</form>
<!-- if I write the second form normally, it work fine, but by onclick event to add form, it cannot upload.
<form id="uploadfree" class="dropzone" action="test2.php" method="post" style="width:400px;" >
<div class="dropzone-previews"></div> <!-- this is were the previews should be shown. -->
<!-- Now setup your input fields -->
<input type="text" name="free" />
<button type="submit">Submit data and files!</button>
</form> -->
</div>
</body>
</html>
【问题讨论】:
-
您需要为运行时添加的每个表单初始化 dropzone
-
如果我正常写第二个表单,它工作正常,但是通过onclick事件添加表单,它无法上传。
标签: javascript jquery html dropzone.js