目前正在从事个人项目。我希望用户单击一个按钮,系统会显示一个 SweetAlert 提示,供用户验证其凭据。但是,我在 SweetAlert 网站上看到的代码只允许一个输入字段。这是我的代码:
swal({
title: "Authenicating for continuation",
text: "Test",
type: "input",
showCancelButton: true,
closeOnConfirm: false,
animation: "slide-from-top",
inputPlaceholder: "Write something"
}, function(inputValue) {
if (inputValue === false) return false;
if (inputValue === "") {
swal.showInputError("You need to write something!");
return false
}
// swal("Nice!", "You wrote: " + inputValue, "success");
});
那么,有没有办法让我获得两个输入字段?一个输入字段用于密码,另一个输入字段用于文本。
最佳答案
现在 SweetAlert2 可用: https://sweetalert2.github.io
根据他们在底部的信息:
Multiple inputs aren't supported, you can achieve them by using html and preConfirm parameters. Inside the preConfirm() function you can pass the custom result to the resolve() function as a parameter:
swal({
title: 'Multiple inputs',
html:
'<input id="swal-input1" class="swal2-input">' +
'<input id="swal-input2" class="swal2-input">',
preConfirm: function () {
return new Promise(function (resolve) {
resolve([
$('#swal-input1').val(),
$('#swal-input2').val()
])
})
},
onOpen: function () {
$('#swal-input1').focus()
}
}).then(function (result) {
swal(JSON.stringify(result))
}).catch(swal.noop)
关于javascript - 带有两个输入字段的 SweetAlert 提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31463649/