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
4 Answers
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
版权声明:本文标题:html - Javascript using Confirm to cancel form submission- Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742388801a2465575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论