简体   繁体   English

单击确认对话框的“取消”按钮时页面重定向

[英]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.

相关问题 带确认和取消按钮的计时器 - Timer with confirm and cancel button 单击取消按钮后保持当前页面 - stay in the current page after clicking cancel button 单击按钮后刷新页面(确定/取消) - Refresh page after clicking button (ok / cancel) 通过在确认框中单击取消关闭我的父窗口。 我希望取消时它应该在同一页上 - By clicking on cancel in confirm box close my parent window . I want that on cancel it should be on same page 当我单击注销按钮时,会打开确认警报。 当我取消时,它将重定向登录页面,但我想重定向当前页面。 我该怎么做? - When i click in logout button open a confirm alert. When i cancel it will redirect login page but i want to redirect current page. How i can do it? 在“确认”对话框中单击“取消”按钮时,如何打开其他页面? - how to open a different page when cancel button is clicked in the Confirm dialog? 用jQuery单击按钮取消表单并重定向页面 - Button click with jquery cancel form and redirect page 使用确认对话框提交表单时丢失按钮值 - Losing Button value on form submit with Confirm dialogue 确认对话框在“取消”按钮上发布? - Confirm dialog posting on Cancel button? onclick =“ confirm()”取消按钮不起作用 - onclick=“confirm()” cancel button not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM