[英]Page redirect on clicking of “Cancel” button of a confirm dialogue
Please look at the code snippet - 请查看代码段-
<form action="post" onsubmit="return confirm('Are You Sure?')">
<a id="saveBtn" class="btnClass">Save<a/>
</form>
When I click on the 'Save' and then Click on the " Ok " button of confirm popup the form get submitted. 当我单击“保存”,然后单击“ 确定 ”按钮以确认弹出窗口时,将表单提交。 If I click " Cancel " the form is not submitted and the I left on the current page. 如果我单击“ 取消 ”,则不提交表单,并且我离开了当前页面。 Is it possible if I click " Cancel " button then the page redirected to other page? 如果我单击“ 取消 ”按钮,然后页面重定向到其他页面,是否可以?
You should probably not use the onsubmit
attribute, go with the unobtrusive event listener approach for better readability and maintainability. 您可能不应该使用onsubmit
属性,而应采用不引人注目的事件侦听器方法,以提高可读性和可维护性。
Something like this should show the general idea and you can update it with your own redirect link: 这样的内容应该可以显示总体思路,您可以使用自己的重定向链接对其进行更新:
var submitLink = document.getElementById('saveBtn'),
myForm = document.getElementById('myForm');
submitLink.addEventListener('click', function(e) {
if (confirm('Are You Sure?')) {
myForm.submit();
} else {
e.preventDefault();
window.location = 'http://google.com'; // redirect to your own URL here
}
});
This relies on the form and anchor having id
attributes: 这依赖于具有id
属性的表单和锚点:
<form action="post" id="myForm">
<a href="#" id="saveBtn" class="btnClass">Save<a/>
</form>
Yes you can because the confirm will return true/false you can code additional behavior for false result, an example is: 是的,您可以这样做,因为确认将返回true / false,您可以为错误的结果编写其他行为,例如:
<form action="post" onsubmit="return confirm('Are You Sure?')? true:window.location.assign('http://www.w3schools.com')">
<a id="saveBtn" class="btnClass">Save<a/>
You can handle the confirm dialog by equating the state to true or false. 您可以通过将状态等同为true或false来处理确认对话框。
function myFunction() {
var r = confirm('Are You Sure?');
if(r==true)
return true;
else {
event.preventDefault();
window.location="http://example.com";
}
}
Here is a working fiddle https://jsfiddle.net/BoyWithSilverWings/p7knLr3m/ 这是一个有效的小提琴https://jsfiddle.net/BoyWithSilverWings/p7knLr3m/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.