首页 > 解决方案 > 如何让代码执行等到在 Sweetalert 提示符中输入值?

问题描述

我想代码执行等到在甜蜜警报提示中输入一个值。基本上,我想要一个与基本 js 提示相同的甜蜜警报提示的阻止行为。

let value= Swal.fire({
title: "An input!",
text: "Write something interesting:",
input: 'text',
showCancelButton: true        

})

这是将值存储在“值”中的甜蜜警报提示代码。现在,操作是异步的。我希望进一步的代码执行等到我没有得到值。

如果除了 sweet alert 之外还有其他库可以执行预期的行为,我对此很感兴趣。我想要的只是一个带有阻塞行为的风格化提示。

标签: javascriptalertprompt

解决方案


我相信甜蜜的警报可以处理承诺。所以当你执行你的代码时,你可以做

value.then( (foo) => {
// your code goes here after the input is received
})

或者,如果您不想将其存储在变量中而只运行原始的甜蜜警报代码:

Swal.fire({
title: "An input!",
text: "Write something interesting:",
input: 'text',
showCancelButton: true        
})
.then((foo) => {
// your code goes here after the input is received
});

在这种情况下, foo 将是用户提供的输入


推荐阅读