基于 if 语句链接额外的 Sweet Alerts
Chaining extra Sweet Alerts based on if statement
我正在尝试使用 SweetAlert2 创建分步表单,我需要能够在基于 if 语句的链接中添加一个额外的步骤。
例如,在我的第三个模式中,我可能有一个单选问题,上面写着 'Will you be bringing a plus 1?',如果用户选择 'true',我需要它弹出一个额外的阶段,询问 [=20] =],如果用户选择 false 则继续。
swal.mixin({
confirmButtonText: 'Next →',
showCancelButton: true,
progressSteps: ['1', '2', '3', '4']
}).queue([
{
title: 'Which event?',
text: 'Please start by selecting the event you would like to book in!',
input: 'select',
inputClass: 'swal-select-event',
inputPlaceholder: 'Please Select',
inputOptions: {
'1' : 'Dance Event',
'2' : 'Football Event'
},
inputValidator: (value) => {
return new Promise((resolve) => {
if (value === '') {
resolve('You need to select an event!')
} else {
resolve()
}
})
}
},
{
title: 'What Day?',
text: 'Which day are they due to come in?',
html:'<input id="swal-booking-date-select" type="date"/>',
preConfirm: () => {
return document.getElementById('swal-booking-date-select').value
},
inputValidator: (value) => {
return new Promise((resolve) => {
if (value === '') {
resolve('You need to select a date!')
} else {
resolve()
}
})
}
},
{
title: 'Plus one?',
text: 'Will you be bringing a plus one with you?',
input: 'radio',
inputOptions: {
'yes' : 'Yes',
'no' : 'No'
},
inputValidator: (value) => {
return new Promise((resolve) => {
if (value === null) {
resolve('I need to know if you will be bringing a plus 1!')
} else if(value === 'yes') {
//EXTRA STAGE GOES HERE TO GET PLUS ONE NAME
} else {
resolve()
}
})
}
},
{
title: 'What else?',
input: 'text',
text: 'Any other information that needs noting for this booking?'
}
]).then((result) => {
if (result.value) {
//Do something with all of the data here
}
})
有人知道这是否可行吗?
一种方法是为该额外的可选步骤使用一个简单的 Swal 实例...并将该值保存在全局声明的 aside 变量中。
var plus1name="";
为了清楚起见,我不会重复你所有的代码,因为没有改变。这是要添加的新部分:
//EXTRA STAGE GOES HERE TO GET PLUS ONE NAME
swal({
title:"Plus one!",
text:"What is his/her name?",
input:"text"
}).then(function(value){
plus1name = value;
resolve();
});
然后在.then(result)
部分:
//Do something with all of the data here
swalResults = result.value;
swalResults.push(plus1name.value)
console.log(swalResults);
所以你有一个包含所有答案的数组。额外的问题被推到了最后,所以数组中的顺序不是提问的顺序...
我在 CodePen 上处理过它。
我正在尝试使用 SweetAlert2 创建分步表单,我需要能够在基于 if 语句的链接中添加一个额外的步骤。
例如,在我的第三个模式中,我可能有一个单选问题,上面写着 'Will you be bringing a plus 1?',如果用户选择 'true',我需要它弹出一个额外的阶段,询问 [=20] =],如果用户选择 false 则继续。
swal.mixin({
confirmButtonText: 'Next →',
showCancelButton: true,
progressSteps: ['1', '2', '3', '4']
}).queue([
{
title: 'Which event?',
text: 'Please start by selecting the event you would like to book in!',
input: 'select',
inputClass: 'swal-select-event',
inputPlaceholder: 'Please Select',
inputOptions: {
'1' : 'Dance Event',
'2' : 'Football Event'
},
inputValidator: (value) => {
return new Promise((resolve) => {
if (value === '') {
resolve('You need to select an event!')
} else {
resolve()
}
})
}
},
{
title: 'What Day?',
text: 'Which day are they due to come in?',
html:'<input id="swal-booking-date-select" type="date"/>',
preConfirm: () => {
return document.getElementById('swal-booking-date-select').value
},
inputValidator: (value) => {
return new Promise((resolve) => {
if (value === '') {
resolve('You need to select a date!')
} else {
resolve()
}
})
}
},
{
title: 'Plus one?',
text: 'Will you be bringing a plus one with you?',
input: 'radio',
inputOptions: {
'yes' : 'Yes',
'no' : 'No'
},
inputValidator: (value) => {
return new Promise((resolve) => {
if (value === null) {
resolve('I need to know if you will be bringing a plus 1!')
} else if(value === 'yes') {
//EXTRA STAGE GOES HERE TO GET PLUS ONE NAME
} else {
resolve()
}
})
}
},
{
title: 'What else?',
input: 'text',
text: 'Any other information that needs noting for this booking?'
}
]).then((result) => {
if (result.value) {
//Do something with all of the data here
}
})
有人知道这是否可行吗?
一种方法是为该额外的可选步骤使用一个简单的 Swal 实例...并将该值保存在全局声明的 aside 变量中。
var plus1name="";
为了清楚起见,我不会重复你所有的代码,因为没有改变。这是要添加的新部分:
//EXTRA STAGE GOES HERE TO GET PLUS ONE NAME
swal({
title:"Plus one!",
text:"What is his/her name?",
input:"text"
}).then(function(value){
plus1name = value;
resolve();
});
然后在.then(result)
部分:
//Do something with all of the data here
swalResults = result.value;
swalResults.push(plus1name.value)
console.log(swalResults);
所以你有一个包含所有答案的数组。额外的问题被推到了最后,所以数组中的顺序不是提问的顺序...
我在 CodePen 上处理过它。