我尝试使用这个简单的文档示例https://sweetalert2.github.io/ https://sweetalert2.github.io/但我收到错误消息:
未捕获的语法错误: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
处理函数为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)
}
})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<button class="buttonproblem">Click me</button>
请注意,您的const { value: text }
尽管逻辑工作正常,但语法导致了错误,所以我将其删除。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)