甜蜜警报不返回真或假?

Sweet alert not returning true or false?

Sweet-alert 在调用此 get_alert() 函数后未返回 true 或 false 请提出一些建议我们如何才能工作此

function get_alert() {
  $('#removeactive').on('click', function(e) {
    e.preventDefault();
    var message = $(this).data('confirm');

    //pop up
    swal({
        title: "Are you sure ??",
        text: message,
        icon: "warning",
        buttons: true,
        dangerMode: true,
      })
      .then(function(isConfirm) {
        console.log(isConfirm == true);
        if (isConfirm == true) {
          return true;
        } else {
          return false;
        }
      });
  });
}
<button id="removeactive" data-confirm="Are you sure?" type="button">Click</button>

您不需要函数来分配事件处理程序。此外,您在致电 get_alert 时没有告诉我们。调用 get_alert 不会显示警报,只会分配处理程序

这里我运行正在加载页面

如果removeactive元素是动态的,需要改为

$(document).on('click','#removeactive', function(e) {

或更好:

$(document).on('click','.removeactive', function(e) {

所以任何具有 class 的元素都可以调用警报

您还需要删除您知道 isConfirm 状态的活动

这是一个工作示例

$(function() { // on page load
  $('#removeactive').on('click', function(e) {
    e.preventDefault();
    var message = $(this).data('confirm');

    //pop up
    swal({
        title: "Are you sure ??",
        text: message,
        icon: "warning",
        buttons: true,
        dangerMode: true,
      })
      .then(function(isConfirm) {
        console.log("confirmed?", isConfirm);
        if (isConfirm) console.log("deleting"); // here you delete
        else console.log("cancelled"); // here you do whatever or nothing
        // You cannot return anything  
      });
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>

<button id="removeactive" data-confirm="This will remove the active widget from the sprocket" type="button">Click</button>