停止表单提交的 JavaScript 代码
停止表单提交的一种方法是从 JavaScript 函数返回 false.
One way to stop form submission is to return false from your JavaScript function.
单击提交按钮时,将调用验证函数.我有一个表单验证案例.如果满足该条件,我将调用一个名为 returnToPreviousPage();
When the submit button is clicked, a validation function is called. I have a case in form validation. If that condition is met I call a function named returnToPreviousPage();
function returnToPreviousPage() {
window.history.back();
}
我正在使用 JavaScript 和 Dojo 工具包.
I am using JavaScript and Dojo Toolkit.
而不是返回上一页,它提交表单.如何中止此提交并返回上一页?
Rather going back to the previous page, it submits the form. How can I abort this submission and return to the previous page?
推荐答案
可以使用函数的返回值来阻止表单提交
You can use the return value of the function to prevent the form submission
<form name="myForm" onsubmit="return validateMyForm();">
功能类似
<script type="text/javascript">
function validateMyForm()
{
if(check if your conditions are not satisfying)
{
alert("validation failed false");
returnToPreviousPage();
return false;
}
alert("validations passed");
return true;
}
</script>
Chrome 27.0.1453.116 m 如果以上代码不起作用,请设置事件处理程序的参数的 returnValue 字段为 false 以使其工作.
In case of Chrome 27.0.1453.116 m if above code does not work, please set the event handler's parameter's returnValue field to false to get it to work.
感谢 Sam 分享信息.
Thanks Sam for sharing information.
感谢 Vikram 为 validateMyForm() 返回 false 提供的解决方法:
Thanks to Vikram for his workaround for if validateMyForm() returns false:
<form onsubmit="event.preventDefault(); validateMyForm();">
其中 validateMyForm() 是一个在验证失败时返回 false 的函数.关键是使用name事件.我们不能用于例如e.preventDefault()
where validateMyForm() is a function that returns false if validation fails. The key point is to use the name event. We cannot use for e.g. e.preventDefault()
相关文章