Javascript Dropzone.js-从选择框接受的文件不工作
当用户更改从Javascript Dropzone.js-从选择框接受的文件不工作,javascript,dropzone.js,dropzone,Javascript,Dropzone.js,Dropzone,当用户更改从元素中选择的选项时,我希望能够更改Dropzone元素的acceptedFiles选项。有点像这样的代码Dropzone.options.myDropzone={acceptedFiles:selected_value} 到目前为止,我已经尝试了两种方法: HTML选择框: <select class="form-control" id="cond_file_type" name="cond_file_type"> <option>-- File Ty
元素中选择的选项时,我希望能够更改Dropzone元素的acceptedFiles
选项。有点像这样的代码Dropzone.options.myDropzone={acceptedFiles:selected_value}代码>
到目前为止,我已经尝试了两种方法:
HTML选择框:
<select class="form-control" id="cond_file_type" name="cond_file_type">
<option>-- File Type --</option>
<option value="pdf">PDF</option>
<option value="docx">DOCX</option>
<option value="xlsx">XLSX</option>
<option value="png">PNG</option>
<option value="jpg">JPG</option>
<option value="gif">GIF</option>
</select>
我尝试了1和2,但它们都不起作用。这里的问题是,这两个选项都涉及在上次读取变量后更改变量。选项2在这方面最具启发性;执行脚本时,会发生以下情况:
// file_type is set to ''
var file_type = '';
document.getElementById("cond_file_type").onchange = function () {
// This line is not run until later when the user changes the select box.
file_type = '.' + this.options[this.selectedIndex].value;
};
// So file_type is still set to ''
Dropzone.options.myDropzone = {
// accepted files is set to '', regardless of what the user later selects.
acceptedFiles: file_type,
};
但是,即使在选项1中,Dropzone.options
对象也只有在第一次创建Dropzone时才被读取,因此一旦页面加载,编辑它仍然没有意义
我无法立即从Dropzone的文档中看到创建Dropzone后编辑配置的方法,因此我建议您每次都使用新选项重新创建一个新的Dropzone,大致类似(未测试的代码):
非常感谢。我希望dropzone只接受选择框中的文件类型。你有什么解决办法吗?
// file_type is set to ''
var file_type = '';
document.getElementById("cond_file_type").onchange = function () {
// This line is not run until later when the user changes the select box.
file_type = '.' + this.options[this.selectedIndex].value;
};
// So file_type is still set to ''
Dropzone.options.myDropzone = {
// accepted files is set to '', regardless of what the user later selects.
acceptedFiles: file_type,
};
document.getElementById("cond_file_type").onchange = function () {
// This line is not run until later when the user changes the select box.
var file_type = '.' + this.options[this.selectedIndex].value;
var myDropzone = new Dropzone("div#myDropzonesId", {
acceptedFiles: file_type,
url: "/file/post"
});
};