我需要创建一个类似于jquery中的confirm函数的泛型函数,使用sweet alert 2。
<form action="Pros.php" method="post">
<div class="form-group">
<label for="ordID">Email address:</label>
<input type="text" class="form-control" id="ordID">
</div>
<button type="reset" class="btn btn-default">Submit</button>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
问题在于调用函数&#34; ConfirmModalDelete&#34; jquery不等待&#34; swal&#34;的结果。
var eval = ConfirmModalDelete("mensaje text");
alert(eval);
结果总是&#34;假&#34;。
请帮助。
------------------------------------- Edit 09-07-2018 ---- -------------------- 找到了! 解决方案是:
function ConfirmModalDelete(mensaje) {
var retorno = false;
swal({
title: 'Are you sure?',
text: mensaje,
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function (result) {
if (result.value) {
retorno = true;
}
});
return retorno;
}
然后:
var fun = function()
{
alert("hello");
}
MensajeConfirm(fun);
答案 0 :(得分:1)
创建一个回调函数并将其传递给您的函数。
function handleOkClick(){
// do your stuff
}
function ConfirmModalDelete(mensaje, handleOkClick) {
swal({
title: 'Are you sure?',
text: mensaje,
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function (result) {
if (result.value) {
handleOkClick();
}
});
答案 1 :(得分:0)
请使用&#39; .done&#39;功能: -
$("button").click(function() {
swal({
title: 'Are you sure?',
text: 'mensaje',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
} ).then(function (hello) {
alert('dfasdfsd');
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert2@7.12.10/dist/sweetalert2.all.js"></script>
<button type="button">Click Me!</button>
&#13;
答案 2 :(得分:0)
确保您使用的是SweetAlert2的最新版本。您的代码示例工作正常:
console.log(swal.version)
swal({
title: 'Are you sure?',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function (result) {
console.log(result)
});
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@7"></script>
PS。请注意,SweetAlert2与SweetAlert略有不同,请查看简单的迁移指南:https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2