为了账号安全,请及时绑定邮箱和手机立即绑定

使用ajax和sweet alert调用函数

使用ajax和sweet alert调用函数

阿波罗的战车 2022-07-15 09:51:36
我不明白单击 btn 文本时如何调用此函数。唯一有效的方法是(async () => { 我尝试过这个$(document).ready(function() {    $('#demo').submit(function(e) {但不起作用。(async () => {const { value: file } = await Swal.fire({  title: 'Select image',  input: 'file',  inputAttributes: {    'accept': 'image/*',    'aria-label': 'Upload your profile picture'  }})if (file) {  const reader = new FileReader()  reader.onload = (e) => {    Swal.fire({      title: 'Your uploaded picture',      imageUrl: e.target.result,      imageAlt: 'The uploaded picture'    })  }  reader.readAsDataURL(file)}})()<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script><button class="form-control cal "  id="demo">          Try me!        </button>
查看完整描述

1 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

只需将模式放入一个函数中,然后在需要时调用它。此示例显示初始调用和 onclick 调用。


const modal = async function () {


const { value: file } = await Swal.fire({

  title: 'Select image',

  input: 'file',

  inputAttributes: {

    'accept': 'image/*',

    'aria-label': 'Upload your profile picture'

  }

})


if (file) {

  const reader = new FileReader()

  reader.onload = (e) => {

    Swal.fire({

      title: 'Your uploaded picture',

      imageUrl: e.target.result,

      imageAlt: 'The uploaded picture'

    })

  }

  reader.readAsDataURL(file)

}


};


// initial modal call

modal();


// even modal call

$("#demo").click(modal);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>


<button class="form-control cal "  id="demo">

          Try me!

        </button>


查看完整回答
反对 回复 2022-07-15
  • 1 回答
  • 0 关注
  • 139 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号