Sweetalert 2 文本区域异步
Sweetalert 2 textarea async
我尝试使用这个简单的文档示例 https://sweetalert2.github.io/ 但我收到错误消息:
Uncaught SyntaxError: await is only valid in async function
$(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 }
语法导致了错误,尽管逻辑工作正常,所以我删除了它。
我尝试使用这个简单的文档示例 https://sweetalert2.github.io/ 但我收到错误消息:
Uncaught SyntaxError: await is only valid in async function
$(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 }
语法导致了错误,尽管逻辑工作正常,所以我删除了它。