单击甜蜜警报的确认按钮时如何保持同一页面?

时间:2016-11-02 10:05:05

标签: javascript jquery html sweetalert

这是我正在使用的甜蜜警报,当我点击它时,我正在将按钮的颜色从红色变为绿色。但是当点击确认按钮时我不希望页面重新加载。问题是当我点击确认按钮时,甜蜜警报没有关闭。非常感谢任何帮助

 function UpdateChildStatus(id,status)
         {
             $.ajax({
               method:'get',
               url:'updateChildrenStatus',
               data:{id:id,status:status},
               success:function(result){
                    if (status===1)
                    {
                        swal({
                            title: "Successfully Enabled!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonColor: "#2ECC71",
                            confirmButtonText: "Ok",
                            closeOnConfirm: true },
                                function (confirm) {
                                    location.reload();
                                });
                    }

                    else
                    {
                        swal({
                            title: "Successfully Disabled!",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#E74C3C",
                            confirmButtonText: "Ok",
                            closeOnConfirm: true },
                                function (confirm) {
                                     $("#'id'").attr('class', 'btn btn-block btn-success');
                                     event.preventDefault();


                                });
                    }
             },

            error:function(x, y, thrownError){
                    console.log(thrownError);
            }
         });
     }

1 个答案:

答案 0 :(得分:0)

如果你从你的html调用这个函数:

<button onclick="return someFunc()">

您需要将其更改为

return false;

另外,在javascript函数的最后,添加:

jQuery.ajax({
  type: 'POST',
  url: 'https://xxxxxxxxxxx.com/charge.php',
  data: {
    tokenid: token.id,
    email: customer_email,
    amount: amount,
    description: customer_first_name + ' ' + customer_surname + ' | ' + reference
  },
  dataType: 'json',
  success: function(response) {
    alert(response);

    if (response == "OK") {
      alert('Payment successfully made! ');
    } else {
      alert('Payment could not be processed. Please try again.');
      location.reload();
    }
  }
});

这应该足以阻止页面在点击时重新加载。如果您可以提供一些详细的代码,我可以为您修改它。 :)