我想制定一个条件并为每个条件调用一个 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..'
);
}
}
2021 年更新:
只要让你的功能async
and await
来自的承诺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:
There's swal.queue() https://sweetalert2.github.io/#chaining-modals,将其用于多种模式。
您的案例应该如下所示:
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)