1

当用户提交表单时,我希望 ajax 使用以下命令检查电子邮件地址是否存在

$.post("checkemail.php", {email: email})

根据我将选择提交或不提交的值(preventdefault)

但是,表单在返回值之前提交。我尝试了以下方法:

$.when($.post("inc/checkemail.php", {email: email})).done(function(result) {
 // code here
}

但是我似乎无法做到这一点。我究竟做错了什么?

提前感谢您的帮助,你们总是很棒!

4

3 回答 3

4

无论帖子返回什么,都应该始终停止默认行为。只有在“完成”时您才决定提交表单:

$('form').on('submit', function(e) { 
  e.preventDefault()
  $.post('checkemail.php', {email: email})
  .done( function(result){
    // check response to see if the email does not exist
    $('form').off('submit') // We don't want to stop the submission anymore.
    $('form').submit()
  })
})

编辑:另外,请注意 $.post 返回一种延迟对象,因此 $.when 调用是不必要的。

于 2012-12-08T18:37:01.287 回答
1

将依赖于响应的代码放入checkmail.php回调中$.post

$.post("checkemail.php", {email: email}, function(result){
    // "result" = response from checkmail.php
    ...
});

干杯

于 2012-12-08T18:32:28.510 回答
0
$.post("checkemail.php", {email: email}, function(data, textStatus, jqXHR) {
    // this gets executed when the AJAX has finished and had succes.
});

直接来自文档:http ://api.jquery.com/jQuery.post/ 。

于 2012-12-08T18:33:46.853 回答