你好我正在與[dropzone.js]
(拖放文件庫) 我想用戶可以點擊一些按鈕和Dropzone表單將被添加。 (我已經有1個dropzone表單,用戶點擊添加後,將會顯示第二個Dropzone表單。)是否有可能通過onclick添加dropzone表單javascript
但是我的問題是點擊添加按鈕後,dropzone表單添加成功,但無法上傳文件。我應該如何使它像第一個Dropzone表單一樣上傳? (哦,如果我正常寫第二個dropzone表單,它可以正常工作,但是如果onclick添加第二個dropzone表單則不起作用。)
這是我的代碼。
<!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>
如果我正常編寫第二個表單,它會正常工作,但是通過onclick事件添加表單,它不能上載,您需要爲運行時添加的每個表單初始化dropzone –
。 – doflamingo