简体   繁体   English

sweetalert:如何传递参数回调

[英]sweetalert: how pass argument to callback

I am using javascript alert library sweetalert 我正在使用javascript警报库sweetalert

My code is: 我的代码是:

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");
    });
}

How can I pass id from foo() function to callback function in swal? 如何将idfoo()函数传递给swal中的回调函数?

function(id){
  alert(MyId);
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
});

This will not work, because in this case id is the option isConfirm for your confirmation dialog - see SweetAlert Documentation . 这不起作用,因为在这种情况下, id是确认对话框的选项isConfirm - 请参阅SweetAlert文档

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);

here the jsfiddle: http://jsfiddle.net/60bLyy2k/ 这里是jsfiddle: http//jsfiddle.net/60bLyy2k/

just put your parameter in local variable they are accessible in inner function or in clousers 只需将您的参数放在局部变量中,它们可以在内部函数或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);

here the fiddle https://jsfiddle.net/ 这里的小提琴https://jsfiddle.net/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM