简体   繁体   中英

Javascript confirm Stop page refresh after cancel

I have made a JavaScript function which is attached to the cancel button on of a form. The issue I am finding is that when the cancel button is pressed the page/form reloads losing the data in the text fields.

    function cancelConfirm(){
    var confirmCancel = confirm("Are you sure you wish to cancel?");
    if(confirmCancel==true)
    {
    alert("byebye");
    }
    else
    {
    return false;
    }
    };

I was just wondering how can you prevent the page from reloading after the cancel button on the confirm has been clicked? Thanks for any help you can give

function cancelConfirm() {
    var confirmCancel = confirm("Are you sure you wish to cancel?");
    if (confirmCancel == true) {
        alert("byebye");
        return false;// to stop postback on ok cick of confirmation popup
    }
    else {
        return false;
    }
}

你可以用一个简单的方法

<a href="" onclick="return confirm('Are you sure!!!!')">Delete </a>

What you can do is to add an onClick event and pass the event object down to it.

{onClick(e) => {
  e.preventDefault();
  //do something here
}}

A simple e.preventDefault() is what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM