我正在使用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");
    });
}

如何将idfoo()函数传递到swal中的回调函数?

最佳答案

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

这将不起作用,因为在这种情况下, id 是您的确认对话框的选项 isConfirm -请参见SweetAlert Documentation

这将起作用-不需要其他变量:
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/

关于javascript - sweetalert : how pass argument to callback,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32218861/

10-09 17:49