Sweetalert 2 textarea异步 [英] Sweetalert 2 textarea async

查看:77
本文介绍了Sweetalert 2 textarea异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


未捕获的SyntaxError:await仅在异步函数中有效




  $(document).ready(function(){
$('。buttonproblem')。click(function(){
const {value:text} = await swal({
输入:'textarea',
inputPlaceholder:'在此输入你的信息',
showCancelButton:true
})

if(text){
swal(text)
}
})
});


解决方案

问题是因为你需要声明点击处理程序函数为 async ,以便在中使用关键字它。试试这个:



  $('。buttonproblem')。click(异步函数) (){注意在这里使用'async'const text = await swal({title:'foo',//注意你的示例输入中缺少这个:'textarea',inputPlaceholder:'在这里输入你的消息',showCancelButton :true})if(text){swal(text)}}) 

 < link rel =stylesheethref =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>点击我< / button>  



请注意, const {value:text} 语法导致错误,尽管逻辑工作正常,所以我删除了它。


I tried with this simple example of documentation https://sweetalert2.github.io/ but i get the error message :

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)
    }
  })   
});

解决方案

The issue is because you need to declare the click handler function as async in order to use the await keyword within it. Try this:

$('.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>

Note that your const { value: text } syntax was causing an error, although the logic worked fine, so I removed it.

这篇关于Sweetalert 2 textarea异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆