2017-05-04 42 views
1
返回值

我使用sweetalert插件,顯示通知... 現在我需要從是獲得價值/未確認從sweetAlert

function confirm(message) { 
swal({ 
    title: "Are you sure?", 
    text: message, 
    type: "warning", 
    showCancelButton: true, 
    confirmButtonColor: "#DD6B55", 
    confirmButtonText: "Yes", 
    cancelButtonText: "No", 
    closeOnConfirm: false, 
    closeOnCancel: false 
}, 
function(isConfirm){ 
    if (isConfirm) { 
    return true; 
    } else { 
    return false; 
    } 
}); 
} 

然後

if (confirm("my message")) myFunction(); 

我可以打電話myFunction() from isconfirm swal,但我有很多「myFunction()」,我不想重寫每個這些swal ...

回答

1

選項1: 功能確認需要返回值。 更新一個var並在最後返回它。

選項2: 通過函數作爲要執行的參數並在確認時啓動它。

confirm("my message", myFunction()); 

function confirm(message, FunctionByConfirm) { 

if (isConfirm) { 
    FunctionByConfirm(); 
    } else { 
    // nothing. 
    }