jQuery 从检查值的 ajax 成功函数内部提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6457702/
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
Submit a form from inside an ajax success function that checks the values
提问by denislexic
I've seen about 20 or more posts on the subject, but they either don't make sense to me or slightly different.
我已经看过大约 20 篇或更多关于这个主题的帖子,但它们对我来说要么没有意义,要么略有不同。
It's a pretty simple scenario as well. I have a form, when it submits, I do an ajax call to see if the email is not taken already by another user. If not taken I want to submit the form, if it is, don't submit form.
这也是一个非常简单的场景。我有一个表单,当它提交时,我会调用 ajax 来查看该电子邮件是否已被其他用户接收。如果没有采取我要提交表格,如果是,不要提交表格。
Here's the HTML
这是 HTML
<form id='greatForm' action='godothat.php' method='post'>
<input type='submit' id='email'>
<button>Send</button>
</form>
The JS
JS
$('#greatForm').submit(function(){
// GET THE VARS
var email = $('#email').val();
$.ajax({
data: { 'field1' : email },
type: 'GET',
dataType: 'json',
url: 'url.php',
beforeSend : function(){ },
success: function(answer){
if(answer.error === false){
// Submit this form without doing the ajax call again
}
if(answer.error === true){
// Not ok, don't submit this form
}
}
});
return false;
});
Thanks for any help.
谢谢你的帮助。
** UPDATE ** Maybe I didn't phrase it right. What I mean is if the "answer.error === false" is true then the submit function should return true.
** 更新 ** 也许我的措辞不对。我的意思是如果“answer.error === false”为真,那么提交函数应该返回真。
So on AJAX complete, if answer.error is false, the submit should be true and vice versa...
所以在 AJAX 完成时,如果 answer.error 为假,则提交应为真,反之亦然......
Does that make more sense?
这样做更有意义吗?
回答by joern
Try to remove the "submit" event handler before submitting the form again:
在再次提交表单之前尝试删除“提交”事件处理程序:
if(answer.error === false){
// Submit this form without doing the ajax call again
$('#greatForm').unbind().submit();
}
回答by Jean-Philippe Gire
$('#greatForm').submit(function(){
if(!$(this).attr('validated'))
{
// GET THE VARS
var email = $('#email').val();
$.ajax({
data: { 'field1' : email },
type: 'GET',
dataType: 'json',
url: 'url.php',
beforeSend : function(){ },
success: function(answer){
if(answer.error === false){
$('#greatForm').attr('validated',true);
$('#greatForm').submit();
}
if(answer.error === true){
//display the errors
}
}
});
return false;
}
return true;
});
This should do the trick, you just add a property validated to the form if it's already validated. And you validate the form only if the attribute is not preset otherwise you submit.
这应该可以解决问题,您只需向表单添加一个已验证的属性(如果它已经过验证)。并且仅当属性未预设时才验证表单,否则您提交。
回答by Naomi Fridman
If I understood correctly, you need echo treu/false from your php.url example:
如果我理解正确,您需要从 php.url 示例中 echo treu/false :
$.ajax({
type: 'POST',
url: 'url.php',
data: $("#your_form").serialize(),
success: function(data) {
if (data=='true') {
...
} else {
and in url.php
并在 url.php
echo "true";
echo "false";
回答by Chris Pratt
Technically, you can make it "submit" just by returning true
. The browser will complete its normal form sending procedure. However, if you're want to do it with AJAX, you have to make another AJAX call to send the form. Each request is its own entity. It's not keeping the connection open for you.
从技术上讲,您只需返回true
. 浏览器将完成其正常的表单发送程序。但是,如果您想使用 AJAX 来完成此操作,则必须进行另一个 AJAX 调用来发送表单。每个请求都是它自己的实体。它不会为您保持连接打开。
However, @Dogbert is right. This type of thing should be done server side, once the form data is processed there. If you do that with AJAX you can still use the returned response to notify the user of any errors, but you should expect them to submit the form again once making the necessary corrections.
但是,@Dogbert 是对的。一旦表单数据在那里处理,这种类型的事情应该在服务器端完成。如果您使用 AJAX 执行此操作,您仍然可以使用返回的响应来通知用户任何错误,但您应该期望他们在进行必要的更正后再次提交表单。