如何在点击甜蜜警报的确认按钮时保持在同一页面上

how to stay on the same page when the confirm button of a sweet alert is clicked?

本文关键字:一页 按钮 甜蜜 确认      更新时间:2023-09-26

这是我正在使用的甜蜜警报,我正在改变一个按钮的颜色,从红色到绿色,当它被点击。但我不希望在点击确认按钮时页面重新加载。问题是,当我点击确认按钮时,甜蜜警报没有关闭。如有任何帮助,不胜感激

 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);
            }
         });
     }

如果你从你的HTML中像这样调用函数:

<button onclick="someFunc()">,

你需要把它改成

<button onclick="return someFunc()">

也,在javascript函数的末尾,添加:

return false;

应该足以防止页面在点击时重新加载。如果你能提供一些详细的代码,我可以帮你修改。:)