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

Even after clicking cancel the form gets 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">

我怎样才能避免这种情况?谁能告诉我如何在用户单击取消时阻止提交表单?

@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>

这是有效的代码片段,您的代码也能正常工作:

 <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>

就放

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

取消即可。