sweetalert2 multiple swal at the same function

亡梦爱人 提交于 2020-05-13 19:27:03

问题


I'd like to make a condition and call a swal for each one (Sweetalert2). But only one of the swal runs. How can I do it?

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

回答1:


There's swal.queue(), use it for multiple modals.

Your case should look like this:

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


来源:https://stackoverflow.com/questions/38085851/sweetalert2-multiple-swal-at-the-same-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!