Sweetalert 2 textarea异步

我尝试使用这个简单的文档示例https://sweetalert2.github.io/,但我收到错误消息:

Uncaught SyntaxError:await仅在异步函数中有效

$(document).ready(function() { $('.buttonproblem').click(function() { const { value : text } = await swal({ input: 'textarea', inputPlaceholder: 'Type your message here', showCancelButton: true }) if (text) { swal(text) } }) }); 

问题是因为您需要将click handler函数声明为async ,以便在其中使用await关键字。 试试这个:

 $('.buttonproblem').click(async function() { // note use of 'async' here const text = await swal({ title: 'foo', // Note this was missing in your example input: 'textarea', inputPlaceholder: 'Type your message here', showCancelButton: true }) if (text) { swal(text) } })