简体   繁体   English

Javascript使用确认取消表单提交

[英]Javascript using Confirm to cancel form submission

How can I add a cancel button to an alert window? 如何在警报窗口中添加取消按钮?

I have used the confirm() method but when the submit button is clicked the confirm window pops up, but when clicking on cancel button, the form's data is stored. 我使用了confirm()方法但是当单击提交按钮时会弹出确认窗口,但是当单击取消按钮时,将存储表单的数据。

I just want clicking on the cancel button to not store the data and leave the previous form as it is. 我只想点击取消按钮不存储数据并保留原来的表格。 This is my code: 这是我的代码:

function submitdata()
{   
    var r=confirm("Are You Sure You Want To Proceed?");

    if(r==true)
    {
        alert("Record is saved");
    }
    else
    {
        alert("Cancelling Transaction");
        javascript:history.go(0);
    }   
}
<form action="" name="test" onsubmit="return submitdata();">
    <input type="text" />

    <input type="submit" />

</form>

<script type="text/javascript">

function submitdata() { 
    var r=confirm("Are You Sure You Want To Proceed?"); 
    if(r==true) { 
        alert("Record is saved"); 
    } else { 
        alert("Cancelling Transaction"); 
        javascript:history.go(0);
    }

}

</script>

This is all you need really: 这就是你真正需要的:

<script type="text/javascript">

    function submitdata() { 
        return confirm("Are You Sure You Want To Proceed?"); 
    }

</script>

Update 更新

function submitdata() {
    var r=confirm("Are You Sure You Want To Proceed?");

    if(r==true) {
        alert("Record is saved");
        return true;
    } else {
        alert("Cancelling Transaction");
        return false;
    }   
}

Reverse the logic. 颠倒逻辑。 But the question is weird language so its better to use a custom prompt that sets cancel to the default. 但问题是奇怪的语言,所以最好使用自定义提示设置取消默认值。

function forceNonDefaultOk() {
    return ( ! confirm( 'Keep working' ) );
}

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

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