如何在 MVC 项目中使用 JScript Promises
How to use JScript Promises in an MVC project
我目前正在使用 SweetAlert in my MVC project and it works fine. However, I need to add some radio buttons to it. This feature seems to be part of SweetAlert Version 2。
现在如果我不使用 await 执行以下操作 :
function SampleCode() {
var formValues = await swal.fire({
title: 'Multiple inputs',
html:
'<input id="swal-input1">' +
'<input type="checkbox" id="swal-input2" class="swal2-input">',
focusConfirm: false,
preConfirm: () => {
return [
document.getElementById('swal-input1').value,
document.getElementById('swal-input2').value
];
}
});
//Swal.fire(JSON.stringify(formValues));
}
然后弹出对话框。但是如果我取消对第二个 SweetAlert 的注释,它当然不会起作用,因为我需要使用 promises。我如何在 MVC 项目的 .cshtml 文件中的上述函数中执行此操作?感谢您的帮助。
来自documentation (the section which says "Multiple inputs aren't supported, you can achieve them by using html and preConfirm parameters..."), here's a CodePen showing something closer to what I think you might be trying to do. Looks like you forgot to declare the outer function as being async, which probably is the reason for your error: https://codepen.io/anon/pen/gyNYep?&editable=true
供参考,JavaScript 需要如下所示:
(async function getFormValues () {
const {value: formValues} = await Swal.fire({
title: 'Multiple inputs',
html:
'<input id="swal-input1" class="swal2-input">' +
'<input id="swal-input2" class="swal2-input">',
focusConfirm: false,
preConfirm: () => {
return [
document.getElementById('swal-input1').value,
document.getElementById('swal-input2').value
]
}
})
if (formValues) {
Swal.fire(JSON.stringify(formValues))
}
})()
我目前正在使用 SweetAlert in my MVC project and it works fine. However, I need to add some radio buttons to it. This feature seems to be part of SweetAlert Version 2。
现在如果我不使用 await 执行以下操作 :
function SampleCode() {
var formValues = await swal.fire({
title: 'Multiple inputs',
html:
'<input id="swal-input1">' +
'<input type="checkbox" id="swal-input2" class="swal2-input">',
focusConfirm: false,
preConfirm: () => {
return [
document.getElementById('swal-input1').value,
document.getElementById('swal-input2').value
];
}
});
//Swal.fire(JSON.stringify(formValues));
}
然后弹出对话框。但是如果我取消对第二个 SweetAlert 的注释,它当然不会起作用,因为我需要使用 promises。我如何在 MVC 项目的 .cshtml 文件中的上述函数中执行此操作?感谢您的帮助。
来自documentation (the section which says "Multiple inputs aren't supported, you can achieve them by using html and preConfirm parameters..."), here's a CodePen showing something closer to what I think you might be trying to do. Looks like you forgot to declare the outer function as being async, which probably is the reason for your error: https://codepen.io/anon/pen/gyNYep?&editable=true
供参考,JavaScript 需要如下所示:
(async function getFormValues () {
const {value: formValues} = await Swal.fire({
title: 'Multiple inputs',
html:
'<input id="swal-input1" class="swal2-input">' +
'<input id="swal-input2" class="swal2-input">',
focusConfirm: false,
preConfirm: () => {
return [
document.getElementById('swal-input1').value,
document.getElementById('swal-input2').value
]
}
})
if (formValues) {
Swal.fire(JSON.stringify(formValues))
}
})()