javascript - 使用ajax和sweet alert调用函数
问题描述
我不明白单击 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>
解决方案
只需将模式放入一个函数中,然后在需要时调用它。此示例显示初始调用和 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>
推荐阅读
- visual-studio - Visual Studio 项目中的源文件列表
- c# - 发布时在服务器上更新连接字符串
- c# - Xamarin iOS 链接器问题
- python - 如果它们存在于另一个文本文件中,则从文本文件中删除它们
- java - TaskExecutor 不工作 Spring Integration
- html - CSS 问题:无法让 UL 元素不换行
- android - 如何将进度对话框设置为两者之间的最大值
- java - 获取 java.lang.IllegalStateException:无法在 Spring Batch 中执行 CommandLineRunner
- javascript - 如何使用 AJAX 从 API 请求数据。
- java - 将 PagedResourcesAssembler 与 List 一起使用
内容