[英]sweetalert: how pass argument to callback
我正在使用javascript警報庫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中的回調函數?
function(id){
alert(MyId);
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
這不起作用,因為在這種情況下, id是確認對話框的選項isConfirm - 請參閱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(isConfirm){
alert(isConfirm);
alert(id);
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
}
foo(10);
這里是jsfiddle: http : //jsfiddle.net/60bLyy2k/
只需將您的參數放在局部變量中,它們可以在內部函數或clousers中訪問
function foo(id) {
var MyId = 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(){
alert(MyId);
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
}
foo(10);
這里的小提琴https://jsfiddle.net/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.