admin管理员组

文章数量:1335402

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.

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

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.

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);
    }   
}
Share Improve this question edited Sep 1, 2012 at 10:26 Scott 21.5k8 gold badges66 silver badges72 bronze badges asked Sep 1, 2012 at 9:48 Ankit SharmaAnkit Sharma 3962 gold badges7 silver badges20 bronze badges 1
  • 2 use return false; in your method to stop execution – swapnesh Commented Sep 1, 2012 at 9:54
Add a ment  | 

4 Answers 4

Reset to default 2
<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' ) );
}

本文标签: htmlJavascript using Confirm to cancel form submission Stack Overflow