示例:安裝sweetalert插件
1.yarn add sweetalert
2.resources/js/bootstrap.js中引入:
require('sweetalert');
$(document).ready(function() { // 刪除按鈕點擊事件 $('.btn-del-address').click(function() { // 獲取按鈕上 data-id 屬性的值,也就是地址 ID var id = $(this).data('id'); // 調用 sweetalert swal({ title: "確認要刪除該地址?", icon: "warning", buttons: ['取消', '確定'], dangerMode: true, }) .then(function(willDelete) { // 用戶點擊按鈕后會觸發這個回調函數 // 用戶點擊確定 willDelete 值為 true, 否則為 false // 用戶點了取消,啥也不做 if (!willDelete) { return; } // 調用刪除接口,用 id 來拼接出請求的 url axios.delete('/user_addresses/' + id) .then(function () { // 請求成功之后重新加載頁面 location.reload(); }) }); }); });