我在如下函式上創建了一個按鈕:
function onDataBound(e) {
$(this).find(".btn-group").append("
<a class='dropdown-item text-danger is-active'
onclick='showSweetAlert(" dataItem.AdvertiserId ", " dataItem.IsActive ")'
type='button'>Mark Inactive
</a>");
});
}
如您所見,它呼叫了一個新函式:
function showSweetAlert(id, isActive){
document.addEventListener('click',function(e)
{
if (isActive) {
Swal.fire
({
title:"Are you sure?",
text:"This will inactivate the advertiser!",
icon:"warning",
showCancelButton:!0,
confirmButtonColor:"#2ab57d",
cancelButtonColor:"#fd625e",
confirmButtonText:"Inactivate"
}).then(function(result){
if(result.isConfirmed){
$.ajax({
url:'/Advertisers/ActiveAdvertiser?id=' id '&isActive=' isActive,
method: 'POST',
success: function(r){
Swal.fire("Inactivated!", "Advertiser inactivated successfully", "success");
},
error: function (request) {
console.log(request.responseText)
Swal.fire("Error!", "Something went wrong, please try again`", "warning");
}
});
}
else{
Swal.fire("Error!", "Something went wrong, please try again", "warning");
}
}
)
}
})
}
此功能顯示甜蜜警報 2
警報有 2 個按鈕,但是當我單擊一個按鈕時問題就開始了,它只是沒有改變狀態,警報只是無限次重新出現,我無法關閉它。我認為問題出在 showSweetAlert 函式呼叫上,但我找不到真正的問題。
uj5u.com熱心網友回復:
問題是您的 showSweetAlert 函式沒有回應點擊。它添加了另一個 onclick 事件處理程式,但這次添加到檔案(整個頁面)。因此,當您第一次單擊時,您將一個事件處理程式添加到回應您給出的單擊的檔案中,并添加另一個警報,您將再次單擊該警報,因為偵聽器在整個頁面中,它會一次又一次地回答。
在這里,嘗試這種方式:
function showSweetAlert(id, isActive){
if (isActive) {
Swal.fire({
title:"Are you sure?",
text:"This will inactivate the advertiser!",
icon:"warning",
showCancelButton:!0,
confirmButtonColor:"#2ab57d",
cancelButtonColor:"#fd625e",
confirmButtonText:"Inactivate"
}).then(function(result){
if(result.isConfirmed){
$.ajax({
url:'/Advertisers/ActiveAdvertiser?id=' id '&isActive=' isActive,
method: 'POST',
success: function(r){
Swal.fire("Inactivated!", "Advertiser inactivated successfully", "success");
},
error: function (request) {
console.log(request.responseText)
Swal.fire("Error!", "Something went wrong, please try again`", "warning");
}
});
} else {
Swal.fire("Error!", "Something went wrong, please try again", "warning");
}
})
}
}
免責宣告:我沒有測驗此代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/522026.html
上一篇:將陣列劃分為組索引
