我有这个问题很长一段时间,直到我想我会解决它。这就是我所做的。
在表单中,我向#upload
和#drop
元素添加了类。我将它们重命名为#upload1
,#upload2
和#drop1
,#drop2
<form id="upload1" class="upload" method="post" action="upload.php" enctype="multipart/form-data">
<div id="drop1" class="drop">
...
在 JS 方面(script.js),我将整个东西包装在一个适当的 jQuery init 中,并在顶部添加了一个 jQuery,并将整个主要区域包装在这个中:
(function($){
$('.upload').each(function (_key, _value) {
var $this = $(this);
var ul = $this.find('ul');
$this.find('#drop a').click(function(){
...
});
})(jQuery);
我还替换了#upload
with 的所有实例和 with的$this
所有实例#drop
$this.find('.drop')
基本上,您正在用类名替换 id,并相应地调整您的脚本,并将其全部包装在一个大的 each 循环中。
PS。我还想在我的脚本文件中添加一个完整的回调,这样我就可以在所有事情之后做一些事情。
complete:function() {
},
请让我知道这是否适合您。
更新:修改代码以动态工作:
(function($){
$(document).ready(function(){
$(document).on('click','.drop a', function(){
var $drop = $(this);
var $this = $drop.closest('.upload');
var ul = $this.find('ul');
$this.parent().find('input').click();
//console.log($this.find('.drop'));
});
window.init_file_upload = function($element) {
// Initialize the jQuery File Upload plugin
$($element).fileupload({
//var $this = $(this);
// This element will accept file drag/drop uploading
dropZone: $element.find('.drop'),
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function (e, data) {
ul = $element.find('ul');
//console.log('adsf');
$('.ajaxform button.submit').attr('disabled','disabled');
var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span><i class="fa fa-check-circle-o"></i> OK</span></li>');
// Append the file name and file size
tpl.find('p').text(data.files[0].name)
.append('<i>' + formatFileSize(data.files[0].size) + '</i>');
// Add the HTML to the UL element
ul[0].innerHTML = '';
data.context = tpl.appendTo(ul);
// Initialize the knob plugin
tpl.find('input').knob();
// Listen for clicks on the cancel icon
tpl.find('span').click(function(){
if(tpl.hasClass('working')){
jqXHR.abort();
}
tpl.fadeOut(function(){
tpl.remove();
});
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
},
progress: function(e, data){
// Calculate the completion percentage of the upload
var progress = parseInt(data.loaded / data.total * 100, 10);
// Update the hidden input field and trigger a change
// so that the jQuery knob plugin knows to update the dial
data.context.find('input').val(progress).change();
if(progress == 100){
data.context.removeClass('working');
}
},
complete:function(e, data) {
// console.log(e,data);
var _data = $.parseJSON(e.responseText);
// console.log(_data);
postAjax(_data);
$('.ajaxform button.submit').removeAttr('disabled');
},
fail:function(e, data){
// Something has gone wrong!
data.context.addClass('error');
}
});
}
$('.upload').each(function() {
window.init_file_upload($(this));
});
// Simulate a click on the file input button
// to show the file browser dialog
// Prevent the default action when a file is dropped on the window
$(document).on('drop dragover', function (e) {
e.preventDefault();
});
// Helper function that formats the file sizes
function formatFileSize(bytes) {
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes / 1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes / 1000000).toFixed(2) + ' MB';
}
return (bytes / 1000).toFixed(2) + ' KB';
}
});
})(jQuery);