如何防止在单击确认警报框中的取消按钮后刷新页面

How to prevent tha page refresh after click the cancel button in confirmation alert box

本文关键字:按钮 取消 刷新 何防止 单击 确认      更新时间:2023-09-26

我正在尝试在单击警报框中的取消按钮后停止页面刷新。这就是我正在尝试的方式

$(".settings").click(function(event){
 var con = (confirm("This some text. Are you sure?"))
 if (con == false)
 {
   return false;
 }
 else
 {
    }   
});

.settingspan tag中的齿轮图标类。请帮助我。谢谢。

如图所示使用preventDefault():-

$(".settings").click(function(event){
   event.preventDefault();
   var con = confirm("This some text. Are you sure?")
   if (con == false){
     // ....
   }
   else{
     // ....
   }
});

或(使用return false

$(".settings").click(function(){
   var con = confirm("This some text. Are you sure?")
   if (con == false){
     // ....
   }
   else{
     // ....
   }
   return false;
});
$(".settings").click(function(event){
   var con = (confirm("This some text. Are you sure?"))
   if (con == true)
   {
      //do something
   }
   else
   {
       //stop page refresh after click cancel
        event.preventDefault();
   }   
});