2

我正在使用 JQuery 表单扩展来提交带有 AJAX 的表单。我有以下代码:

var options = { 
    beforeSubmit:  showRequest,  // pre-submit callback 
    success:       showResponse,  // post-submit callback 

    // other available options: 
    //url:       url         // override for form's 'action' attribute 
    //type:      'post',       // 'get' or 'post', override for form's 'method' attribute 
    //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
    clearForm: true,        // clear all form fields after successful submit 
    //resetForm: true        // reset the form after successful submit 

    // $.ajax options can be used here too, for example: 
    timeout:   3000 
}; 


$('#composeForm').submit(function() { 
    // inside event callbacks 'this' is the DOM element so we first 
    // wrap it in a jQuery object and then invoke ajaxSubmit
 $(this).find(':disabled').removeAttr('disabled');

    $(this).ajaxSubmit(options); 

    // !!! Important !!! 
    // always return false to prevent standard browser submit and page navigation 
    return false; 
}); 

问题是表单似乎没有提交,或者至少没有调用成功函数。如果我删除返回 false,则提交工作,但页面导航离开。我的代码中是否存在可能导致此问题的问题?

干杯,加兹勒。

编辑| 似乎正在我的本地主机上工作。这可能与作为附加域的域有关吗?

4

4 回答 4

3

在这里试试这个:

$(document).ready(function(){
      $("#composeForm").submit(function(){
                var str = $(this).serialize(); 
                var formAction = $(this).attr("action");
                $(this).find(':disabled').attr('disabled','');

$.ajax({
         type: "POST",
         url: formAction,
         data: str,
         beforeSubmit:  showRequest,
         success: showResponse
       });

  return false;

  });

 });
于 2010-03-24T16:10:35.827 回答
2

我的猜测是,当您删除错误时,您的常规提交正在发生——在这两种情况下它都不起作用。

我认为您需要为更多选项设置正确的值。那里有很多东西评论。此外,如果您只使用按钮进行提交而不是表单提交可能会更好,那么您不必担心尝试关闭浏览器执行的自动魔术功能。

于 2010-03-24T15:19:01.897 回答
1

添加自己的解决方案,

iframe 选项需要是:

iframe: true

这解决了这个问题。

感谢大家的意见。

于 2010-03-24T16:15:56.387 回答
0

表单内将有一些字段经过验证并且可能被隐藏,因此请检查所有字段并设置其验证,当我看到由于 jQuery 隐藏显示而隐藏的隐藏字段时,它将为我工作。或者您可以从表单中删除所有验证,它将起作用。

于 2015-09-18T06:13:45.963 回答