Sweetalert阻塞像正常提示

sweetalert blocking like normal prompt

本文关键字:提示 常提示 Sweetalert      更新时间:2023-09-26

我使用swal (http://t4t5.github.io/sweetalert)从用户点击某些东西时获取一些数据。然后我想从我调用的函数中返回它。换句话说,对于下面的示例,我希望将labels中条目的文本设置为输入值。问题是它似乎在关闭/输入数据之前返回:

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

正常的prompt alert阻塞所以可以这样做,swal可以这样做吗?

谢谢

虽然不能使用Sweet Alerts块,但可以使用它的回调功能来触发任何需要先解除警报的代码。例子中有几个这样的例子。例如,假设您有一个是/否样式警告,下面是文件删除示例。

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

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

假设你需要等待,直到有人点击ok。

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

不,它不能,没有办法创建阻塞代码等待用户输入。唯一的方法是JS已经提供的:提示,警报等。只需从回调中运行所需的代码。