sweetalert:如何将参数传递给回调 [英] sweetalert: how pass argument to callback
本文介绍了sweetalert:如何将参数传递给回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JavaScript警报库 sweetalert
I am using javascript alert library sweetalert
我的代码是:
function foo(id) {
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
}
如何将 id
从 foo()
函数传递给swal中的回调函数?
How can I pass id
from foo()
function to callback function in swal?
推荐答案
function(id){
alert(MyId);
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
这将不起作用,因为在这种情况下, id 是用于确认对话框的选项 isConfirm -请参见
This will not work, because in this case id is the option isConfirm for your confirmation dialog - see SweetAlert Documentation.
这将起作用-不需要其他变量:
This will work - no need for an additional variable:
function foo(id) {
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
},
function(isConfirm){
alert(isConfirm);
alert(id);
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
}
foo(10);
此处是jsfiddle: http://jsfiddle.net/60bLyy2k/
here the jsfiddle: http://jsfiddle.net/60bLyy2k/
这篇关于sweetalert:如何将参数传递给回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文