【问题标题】:Sweetalert 2 textarea asyncSweetalert 2 文本区域异步
【发布时间】:2018-04-10 10:15:04
【问题描述】:

我尝试了这个简单的文档示例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)
    }
  })   
});

【问题讨论】:

  • 但我使用了文档的示例及其工作原理? sweetalert2.github.io
  • 能否请您链接到显示此使用模式的文档
  • 为什么需要 await 和 async 来显示消息?

标签: jquery sweetalert2


【解决方案1】:

问题是因为您需要将 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 } 语法导致了错误,尽管逻辑运行良好,所以我将其删除。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-11-30
  • 2015-04-21
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2015-11-10
  • 1970-01-01
相关资源
最近更新 更多