简体   繁体   English

Javascript确认取消后停止页面刷新

[英]Javascript confirm Stop page refresh after cancel

I have made a JavaScript function which is attached to the cancel button on of a form.我制作了一个 JavaScript 函数,它附加到表单的取消按钮上。 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 事件并将事件对象传递给它。

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

A simple e.preventDefault() is what you need.一个简单的e.preventDefault()就是你所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript确认框,如果单击取消,则不会重新加载页面 - Javascript confirm box, if cancel is clicked, it will not reload the page 单击确认框上的取消时页面应刷新 - Page should refresh when click cancel on confirm box 单击“取消Javascript确认”框不会阻止网格刷新 - Clicking on cancel on Javascript Confirm box does not stop the grid from refreshing 单击按钮后刷新页面(确定/取消) - Refresh page after clicking button (ok / cancel) Javascript 确认框,单击取消不起作用,页面仍在加载 - Javascript confirm box, click cancel not working, page still load Javascript确认对话框后,网页转到顶部。 如何停止呢? - After Javascript confirm dialog, web page goes to top. How to stop this? javascript Confirm()取消后,在表单提交按钮上运行AJAX函数 - Run AJAX function on form submit button after javascript confirm() cancel 页面刷新后如何取消刷新开关按钮? - How do I cancel the refresh switch button after a page refresh? 用javascript确认后如何滚动到页面上的锚点 - how to scroll to an anchor on the page after a confirm with javascript 在确认框中单击确认或取消按钮后,如何使用JavaScript或jQuery返回true或false值? - How to return true or false values using JavaScript or jQuery after click on confirm or cancel button from confirm box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM