【问题标题】:sweetalert2 multiple swal at the same functionsweetalert2 多个 swal 在同一功能
【发布时间】:2016-06-28 20:38:10
【问题描述】:

我想创建一个条件并为每个条件调用一个 swal (Sweetalert2)。但只有一只燕子跑。我该怎么做?

function validateEmail(email) {
  var regex = /\S+@\S+\.\S+/;
  return regex.test(email);
}

function validateBirth(data) {
  var regex = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
  return regex.test(data);
}

function validacao() {
  var data = document.getElementById('birth').value;
  var email = document.getElementById('email').value;
  if (!validateBirth(data)) {
    swal(
      'title..',
      'text..',
      'type..'
    );
  }
  if (!validateEmail(email)) {
    swal(
      'title..',
      'text..',
      'type..'
    );
  }
}

【问题讨论】:

    标签: javascript modal-dialog sweetalert sweetalert2


    【解决方案1】:

    2021 年更新:

    只需让你的函数 asyncawait 来自 Swal.fire() 的承诺:

    async function validacao() {
      var data = document.getElementById('birth').value;
      var email = document.getElementById('email').value;
      if (!validateBirth(data)) {
        await Swal.fire(
          'title..',
          'text..',
          'type..'
        );
      }
      if (!validateEmail(email)) {
        await Swal.fire(
          'title..',
          'text..',
          'type..'
        );
      }
    }
    

    旧答案不适用于 SweetAlert2 的最新版本:

    swal.queue(),用于多种模式。

    您的案例应该如下所示:

    var modals = [];
    
    // birth modal
    if (!validateBirth(data)) {
      modals.push({title: 'title1', text: 'text1', ... });
    }
    
    // email modal
    if (!validateEmail(email)) {
      modals.push({title: 'title2', text: 'text2', ... });
    }
    
    Swal.queue(modals);
    

    【讨论】:

    • 不错。我稍后会试试这个。
    • 像魅力一样工作 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多