gpt4 book ai didi

javascript - 成功ajax请求后Blueimp Fileupload触发提交按钮

转载 作者:行者123 更新时间:2023-11-30 10:21:49 24 4
gpt4 key购买 nike

使用 blueimp 的 fileupload 如何在成功的 ajax 请求后触发 fileupload 提交?我试图触发 fileuploadsubmit 但它说数据未定义。下面是初始代码:

HTML:

<form id="add-project-form" method="post" action="http://localhost/project/add/">
<p>
<label for="project">Project Name:</label>
<input id="project" class="input-text" type="text" name="name" />
</p>
<p>
<label for="overview">Project Overview:</label>
<input id="overview" class="input-text" type="text" name="overview" />
</p>
<p><input type="submit" value="Add Project" />
</form>

<form id="logo-upload" method="post" action="http://localhost/project/upload/" enctype="multipart/form-data">
<p>
<input type="file" name="logo" id="logo" />
</p>
</form>

jQuery:

$('#logo-upload').fileupload({
dataType: 'json',
maxNumberOfFiles: 1,
autoUpload: false
}).on('fileuploadsubmit', function(e, data) {
console.log(data);
});


$(document).on("submit", "#add-project-form", function(e) {
e.preventDefault();

var data = $(this).serialize();
var url = $(this).attr("action");

$.ajax({
url: url,
data: data,
type: "post",
dataType: "json",
success: function(response) {
if( response && response.location != undefined ) {

// I would want the upload to begin here

$('#logo-upload').fileupload().trigger('fileuploadsubmit');
}
}
});
});

最佳答案

将提交功能绑定(bind)到按钮上,并在您的 ajax 请求后强制点击:

$('#logo-upload').fileupload({
dataType: 'json',
maxNumberOfFiles: 1,
autoUpload: false,
add : function(e,data){
$("#uploadButton").on("click",function(){
data.submit();
})
}
}).on('fileuploadsubmit', function(e, data) {
console.log(data);
});

$.ajax({
url: url,
data: data,
type: "post",
dataType: "json",
success: function(response) {
if( response && response.location != undefined ) {
$("#uploadButton").click();
}
}
});

关于javascript - 成功ajax请求后Blueimp Fileupload触发提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21241657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com