javascript 如何在ajax调用期间停止表单提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7528234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop form submit during ajax call
提问by vikingmute
I can only modify the code in the ajax call.
The ajax call occurs when I click the submit in form named $('#form1')
.
我只能修改ajax调用中的代码。当我单击名为$('#form1')
.
$('#form1').submit(function () {
$.ajax({
url: 'some.php',
type: 'POST',
data: somedata,
success: function (msg) {
if (!msg) {
// I wanna to stop '#form1' submit here,how to do that? I can only modify the code in the ajax call.
}
}
});
});
回答by John Hoven
You'll need to stop it BEFORE the success handler. Because the function finishes executing after your AJAX call the form will submit while your ajax call is occurring (and by the time your ajax call finishes it is too late).
您需要在成功处理程序之前停止它。因为函数在您的 AJAX 调用后完成执行,所以表单将在您的 ajax 调用发生时提交(当您的 ajax 调用完成时为时已晚)。
But yes, put return false at the end of your function.
但是,是的,将 return false 放在函数的末尾。
function SubmitHandler(){
// Your AJAX call here
// blah blah blah
return false; // Stop form submit
}
If it's really important that it is in the success handler, then you could execute the call synchronously.
如果它在成功处理程序中真的很重要,那么您可以同步执行调用。
回答by Thu marlo
回答by voigtan
return false or event.preventDefault should help you:
return false 或 event.preventDefault 应该可以帮助您:
$('#form1').submit(function(){
$.ajax({
url:'some.php',
type:'POST',
data:somedata,
success:function(msg){
if(!msg){
//i wanna to stop form1 submit here,how to do that? you can only modify the code in the ajax call
}
}
});
return false;
});
or:
或者:
$('#form1').submit(function(e){
e.preventDefault();
$.ajax({
url:'some.php',
type:'POST',
data:somedata,
success:function(msg){
if(!msg){
//i wanna to stop form1 submit here,how to do that? you can only modify the code in the ajax call
}
}
});
});
回答by Mika Andrianarijaona
Put a
放一个
return false;
This should stop the function to continue
这应该停止该功能以继续
回答by Mikhail Kislitsyn
You need to prevent form submitting
您需要阻止表单提交
onclick="ajaxcall(); return false;"
And do it in your code:
并在您的代码中执行此操作:
$.ajax({
url:'some.php',
type:'POST',
data:somedata,
success:function(msg){if(msg) $('#form1').submit();}
})
回答by A.Aleem11
You can change button type from "submit" to "button" then submit form using jquery form.submit(); function according to your conditions. Consider the following example code below:
您可以将按钮类型从“提交”更改为“按钮”,然后使用 jquery form.submit() 提交表单;根据您的条件运行。考虑下面的示例代码:
<script>
$(document).ready(function() {
$("#button-id").click(function() //on click at button
{
var sel=$("#input").val();
$.post("ajax-war.php",{jid:sel,rand:Math.random() } ,function(data)
{
if(data=='valid') //if correct login detail
{
$('#war_form').submit(); //Submit form
}
else
{
alert('not valid'); //will not submit form
}
});
});
});
</script>
回答by Pat
Assuming the form's ID is #form1
, you could bind a submit handler and return false from it:
假设表单的 ID 是#form1
,您可以绑定一个提交处理程序并从中返回 false:
$.ajax({
url:'some.php',
type:'POST',
data:somedata,
success:function(msg){
if(!msg){
$('#form1').submit(function(){
return false; // prevents form submit
});
}
}
})