简体   繁体   English

即使在单击取消后,表单也会被提交

[英]Even after clicking cancel the form gets submitted

I am doing a form submission, and I am displaying a confirmation box asking the user that if they really want to continue .我正在提交表单,并显示一个确认框,询问用户是否真的想继续 But even if the user clicks on cancel, the form is still getting submitted.但即使用户点击取消,表单仍然会被提交。

 <form onsubmit="return confirm('If you enter the incorrect number, the search will still be conducted and charge you for the cost of the search. do you want to continue?.')" action="http:example.com/" id="customersearch" name="customersearch" role="form" method="post" accept-charset="utf-8" novalidate="novalidate">

How can I avoid this?我怎样才能避免这种情况? Can anyone kindly tell me how to stop the form from getting submitted when the user clicks cancel?谁能告诉我如何在用户单击取消时阻止提交表单?

@Akshay Vasu, try with below solution, you need to call onsubmit event of form @Akshay Vasu,尝试使用以下解决方案,您需要调用表单的onsubmit事件

 function validateForm() { if(confirm('If you enter the incorrect number, the search will still be conducted and charge you for the cost of the search. do you want to continue?.')) { $("myform").submit(); } else{ alert("Invalid"); return false; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="myform" name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post"> Name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> 

Here is working code snippet your code is also working fine: 这是工作代码段,您的代码也可以正常工作:

  <form onsubmit="return confirm('If you enter the incorrect number, the search will still be conducted and charge you for the cost of the search. do you want to continue?.')" action="http:example.com/" id="customersearch" name="customersearch" role="form" method="post" accept-charset="utf-8" novalidate="novalidate"> <input type="submit" value="submit"> </form> 

just put就放

<button type="button">Cancel</button>

for cancel and it will work.取消,它将起作用。

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

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