像普通提示一样阻止 sweetalert

sweetalert blocking like normal prompt

我正在使用 swal (http://t4t5.github.io/sweetalert) 在用户单击某些内容时从他们那里获取一些数据。然后我想 return 从我调用 swal 的函数中获取。换句话说,对于下面的示例,我想将 labels 中的项目文本设置为输入值。问题是似乎 return 在 swal closed/data 之前输入:

.on('dblclick', function(l) {
  labels.text(function (e) {  
     return swal({   
      title: "Edit label"
    },
      function(inputValue){  
      if(inputValue) {
        return inputValue
      }
    });
   })
});

一个正常的prompt alert阻塞所以可以做到,swal可以做到吗?

谢谢

不可以,无法创建等待用户输入的阻塞代码。只有 JS 已经提供的方法:提示、警报等。只需 运行 您需要的回调代码即可。

虽然您不能设置 Sweet Alerts 块,但您可以使用其回调功能来触发任何需要首先解除警报的代码。这些例子有几个这样的例子。例如,假设您有一个 yes/no 样式的警报,这里有文件删除示例。

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!",
  cancelButtonText: "No, cancel plx!",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  //The callback will only fire on cancel if the callback function accepts an
  //argument. So, if the first line were 'function () {' with no argument, clicking
  //cancel would not fire the callback.
  if (isConfirm) {
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
  } else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
  }
});

或 AJAX 示例:

swal({
  title: "Ajax request example",
  text: "Submit to run ajax request",
  type: "info",
  showCancelButton: true,
  closeOnConfirm: false,
  showLoaderOnConfirm: true,
},
function(){
  setTimeout(function(){
    swal("Ajax request finished!");
  }, 2000);
});

在这两种情况下,直到与警报交互时才会触发回调,并且调用的结果作为参数传递给回调。

所以说你需要等到有人点击好。

swal({
  title: "Delete your account?",
  text: "Clicking on continue will permanently delete your account.",
  type: "warning",
  confirmButtonText: "Continue",
  closeOnConfirm: false
}, function () {
  swal("Deleted account", "We'll miss you!", "success");
});

注意: 如果您要在回调中显示后续警报,则 closeOnConfirm/closeOnCancel 只需 false。如果它设置为 true,它将在向用户显示之前关闭第二个警报。但是,如果您正在做一些与 swal 无关的事情,并且您没有关闭它,它将无限期地保持打开状态。

swal({
  title: "Delete your account?",
  text: "Clicking on continue will permanently delete your account.",
  type: "warning",
  confirmButtonText: "Continue"
}, function () {
  console.log("This still shows!")
});

如果您希望在执行与swal 无关的操作时保持打开警报,您应该在代码末尾调用swal.close()

swal({
  title: "Delete your account?",
  text: "Clicking on continue will permanently delete your account.",
  type: "warning",
  confirmButtonText: "Continue",
  closeOnConfirm: false
}, function () {
  console.log("This still shows!");
  setTimeout(function () {
    // This will close the alert 500ms after the callback fires.
    swal.close();
  }, 500);
});