我有这个代码:
$("#uploadMe").change(function(e) {
var input = $(this);
var file = input[0].files[0];
var fileName = file.name;
var template = '<div class="j_upload_item">' +
'<span class="j_label">Uploading <pre></pre></span>' +
'<div class="j_upload_progress progress progress-striped active">' +
'<div class="bar" style="width: 0%"></div>' +
'</div>' +
'</div>';
box = $("#j_upload_box").append(template);
box.hide();
box.fadeIn(200);
box.find('pre').text(fileName);
bar = box.find('.bar');
formData = new FormData($('form')[1]);
$.ajax({
url: '/admin/callbacks/upload.php?type=email-attachment', //server script to process data
type: 'POST',
xhr: function() { // custom xhr
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // check if upload property exists
myXhr.upload.addEventListener('progress',progressHandler, false); // for handling the progress of the upload
}
return myXhr;
},
//Ajax events
beforeSend: beforeSendHandler,
success: successHandler,
error: errorHandler,
// Form data
data: formData,
//Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false
});
function beforeSendHandler(e) {
bar.css('width', '0%');
}
function successHandler(data) {
data = $.parseJSON(data);
bar.parent('div').addClass('progress-success');
bar.css('width', '100%').delay(3000).queue(function(next) {
box.fadeOut(500);
next();
});
var curval = $("#j_attachments").val();
var arr = $.makeArray(curval);
arr.push(data.name);
$("#j_attachments").val(arr);
}
function errorHandler(e) {
console.log(e);
}
function progressHandler(e) {
bar.css('width', ((e.loaded / e.total) + '%'));
}
});
$("uploadMe")
是文件输入。该脚本应该上传文件并显示一个由template
. 而不是像他们应该的那样淡出并消失的盒子,每次change
触发事件时它们都会不断弹出。
所以发生的情况是,我最终得到了 3 个堆叠在一起的盒子,里面有相同的东西。
如果您需要澄清,请询问。这真的让我很烦:(