我正在为我的项目使用Sonata Admin,我在$('button[type="submit"]')
事件中有一个SweetAlert 2。
问题在于,我最初使用preventDefault()
来保持表单活着,然后我尝试使用$(form).submit()
提交表单但我已重定向到同一个表单编辑页面,即使我点击"更新&关闭"页。
仅供参考:Sonata有两种可能性:'更新' &安培; '更新并关闭'。第一个是更新实体并将您重定向到同一页面,另一个是更新实体并将您重定向到列表页面。
$('button[type="submit"]').click(function (e) {
var currentStatus = $('#' + adminUniqueId + '_status option:selected').val();
var exitDate = $('#' + adminUniqueId + '_exitDate').val().trim();
var form = $(this).parents('form');
var nextPage = $(this).attr('name');
loanId = parseInt(loanId);
// Modal operations
if((currentStatus === closedStatus || exitDate !== '') && loanId !== 0) {
if (outstandingBalance > 0) {
e.preventDefault(); // Stop action until we find a response.
swal({
title: 'Current outstanding principal balance is $' + numberFormatter.format(outstandingBalance) + '. How much do you want to write off ?',
input: 'text',
width: '600px',
inputClass: 'money-mask',
showCancelButton: true,
confirmButtonText: 'Close this loan',
showLoaderOnConfirm: true,
preConfirm: function (amount) {
return new Promise(function (resolve, reject) {
var convertedAmount = getValueFromFormattedNumber(amount);
if (isNaN(convertedAmount)) {
reject('Please write an amount.')
} else if (convertedAmount < 0 || convertedAmount > outstandingBalance) {
reject('The write-off amount is greater than the outstanding principal balance.')
} else {
$.ajax({
type: "POST",
url: setLoanTransactionRoute,
data: {
loanId: loanId,
outstandingBalance: outstandingBalance,
amountToWriteOff: convertedAmount
},
success: function (data) {
if (true === data.success) {
$('#' + adminUniqueId + '_status').val(closedStatus).change();
setTimeout($(form).submit(), 500);
resolve();
} else {
reject('We found an error: ' + data.message + '.')
}
},
error: function (response, textStatus, jqXHR) {
reject("Something went wrong and we couldn't add this transaction. Please check if you have a write-off and/or repayment loan category.")
}
});
}
})
}
});
}
}
});
答案 0 :(得分:0)
当用户点击[更新]按钮时,它将重定向到同一个编辑页面,当用户点击[更新并关闭按钮]时,将重定向到列表页面
正如swal
插件所述,您可以在then
swal
函数回调时调用延迟回调preConfirm
:
https://limonte.github.io/sweetalert2/
这是给定的参考代码:
注意:我为此给定表单添加了[id]属性以及一些其他条件
$('#frm-submit input[type="submit"]').click(function(event) {
event.preventDefault();
var aValidSubmitValues = ['Update', 'Update and Close'];
var sButtonValue = $(this).val();
if ($.inArray(sButtonValue, aValidSubmitValues) < 0) {
return false;
}
var convertedAmount = '123.00';
var outstandingBalance = '200.00';
swal({
title: 'Current outstanding principal balance is $100.00 How much do you want to write off ?',
input: 'text',
width: '600px',
inputClass: 'money-mask',
showCancelButton: true,
confirmButtonText: 'Close this loan',
showLoaderOnConfirm: true,
preConfirm: function(amount) {
return new Promise(function(resolve, reject) {
if (isNaN(convertedAmount)) {
reject('Please write an amount.')
} else if (convertedAmount < 0 || convertedAmount > outstandingBalance) {
reject('The write-off amount is greater than the outstanding principal balance.')
} else {
// Your given ajax code up until the resolve callback parameter
// Your $.ajax() here until resolve
resolve();
}
});
}
}).then(function(response) {
console.log('You have click the ' + sButtonValue + ' button');
if (sButtonValue === 'Update') {
console.log('Reload the page');
// location.reload()
} else {
console.log('Go to a certain listing page');
// location.href = '/list-of-transaction-page';
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.11.5/sweetalert2.all.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<form id="frm-submit">
<input type="submit" value="Update" />
<input type="submit" value="Update and Close" />
</form>
</div>
希望这会很好地指导你