首页 > 解决方案 > 如何使用 JQuery 在 SWAL 中成功调用函数?

问题描述

第二个功能不起作用。它没有收到从第一个函数传递的 id。

这是我的 JQuery 代码。

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this record!",
    type: "warning",
    showCancelButton: true,
    confirmButtonClass: "btn-danger",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: "No, cancel!",
    closeOnConfirm: false,
    closeOnCancel: false
    },
    function(isConfirm) {
    if (isConfirm) {
      // this.$http.delete();
      window.axios.post(`/delete-sl-gl/${brcode}/${slc}/${slt}/${sle}/${cts}`)
        .then(response => {
        // console.log(response.data.sle)
          if (response.data.success === true) {
            swal('Deleted', response.data.message, 'success'); 
            //console.log(response.data.sle)
            spliceSLE_gl(sle.sle);
          }

        })
        .catch(error =>{
        console.log(error.response)
        });
    } else {
      swal("Cancelled", "Your file is safe :)", "error");
    }

我在spliceSLE_gl(sle.sle)中遇到错误/被忽略;

标签: jquerylaravel-5

解决方案


一个swal实例是一个promise。要根据用户操作添加功能,您必须使用该then()方法。

这是带有链式catch()then()方法的 swal 的结构。

swal({
    // The swal configuration params
  })

  // To avoid getting a "cancel" if user click the close button
  // or clicks on the overlay (outside the swal)
  // "noop" means "no operation".
  .catch(swal.noop)

  // To handle the confirm/cancel buttons
  .then(function (result) {
    if (result) {
      // Your "confirm" function
    }
    if (!result) {
      // Your "cancel" function
    }
  });

推荐阅读