gpt4 book ai didi

javascript - 即使单击“取消”后,表单也会提交

转载 作者:行者123 更新时间:2023-11-28 17:29:48 25 4
gpt4 key购买 nike

我正在提交表单,并显示一个确认框,询问用户是否确实要继续。但即使用户单击“取消”,表单仍然会被提交。

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

关于javascript - 即使单击“取消”后,表单也会提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50734418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com