javascript swal回调未正确启动

67up9zun  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(173)

我有一个代码,我试图调用aweetalert,注意它使用的是sweetalert,而不是sweetAlert2。
这里是代码
JS代码

function showMessage() {
    swal({
        title: "",
        html: 'Update <b>data.</b>',
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "##EF5350",
        confirmButtonText: "Yes, Proceed",
        cancelButtonText: "Cancel",
        reverseButtons: true
    },function(isConfirmed) {
        alert(isConfirmed)
        if (isConfirmed) {
            alert($(".openStandings").attr('data-href'));
            $.ajax({
                type: "POST",
                url: $(".openStandings").attr('data-href'),
                success: function(data) {
                alert(data);
                if (data.toLowerCase().indexOf("error") >= 0) {
                    swal({
                        title: "Oops",
                        text: data,
                        type: "error",
                        showCancelButton: false,
                        confirmButtonColor: "##EF5350",
                        confirmButtonText: "OK",
                        closeOnConfirm: true
                    });
                } else {
                    swal({
                        title: "Great",
                        text: data,
                        type: "success",
                        showCancelButton: false,
                        confirmButtonColor: "##EF5350",
                        confirmButtonText: "OK",
                        closeOnConfirm: true
                    });
                }
            }
            })
        }
    })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>

<button onclick="showMessage()" class="openStandings" data-href="page.cfm">Click Me<button>

对象首先是swal工作,但当它进入回调时,什么也没有发生

yqkkidmi

yqkkidmi1#

SWAL基于Promise;它不直接接受回调。您可以使用.then()

function showMessage() {
  swal({
    title: "",
    html: 'Update <b>data.</b>',
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "##EF5350",
    confirmButtonText: "Yes, Proceed",
    cancelButtonText: "Cancel",
    reverseButtons: true
  }).then(function(isConfirmed) {
    alert(isConfirmed)
    if (isConfirmed) {
      alert($(".openStandings").attr('data-href'));
      $.ajax({
        type: "POST",
        url: $(".openStandings").attr('data-href'),
        success: function(data) {
          alert(data);
          if (data.toLowerCase().indexOf("error") >= 0) {
            swal({
              title: "Oops",
              text: data,
              type: "error",
              showCancelButton: false,
              confirmButtonColor: "##EF5350",
              confirmButtonText: "OK",
              closeOnConfirm: true
            });
          } else {
            swal({
              title: "Great",
              text: data,
              type: "success",
              showCancelButton: false,
              confirmButtonColor: "##EF5350",
              confirmButtonText: "OK",
              closeOnConfirm: true
            });
          }
        }
      })
    }
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>

<button onclick="showMessage()" class="openStandings" data-href="page.cfm">Click Me<button>

相关问题