1. 问题分析
var regData = {url:'...', isCommit:false, data:''};
$('#commitBtn').click(function(){
if (regData.isCommit) {
return false;
}
regData.isCommit = true;
regData.data = new FormData($('#regForm')[0]);
$.ajax({
type: 'POST',
url: regData.url,
data: regData.data,
dataType: 'json',
contentType: false,
processData: false,
success: function (res) {
console.log(res);
regData.isCommit = false;
},
error: function () {
regData.isCommit = false;
}
});
});
2. 解决方案:
if ($('input[name=file]').prop('files').length == 0) {
regData.data = $('#regForm').serialize();
$.ajax({
type: 'POST',
url: regData.url,
data: regData.data,
dataType: 'json',
success: function (res) {
console.log(res);
regData.isCommit = false;
},
error: function () {
regData.isCommit = false;
}
});
} else {
}