Jquery 表单 ajaxSubmit 未提交

发布于 2024-08-26 01:20:10 字数 1265 浏览 5 评论 0原文

我正在使用 JQuery Form 扩展通过 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; 
}); 

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

干杯, 加兹勒。

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

I am using the JQuery Form extension to submit a form with AJAX. I have the following code:

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; 
}); 

The problem is that the form doesn't appear to be submitting, or atleast the success function is not being called. If I remove the return false, then the submission works, but the page navigates away. Is there a problem in my code that could be causing this?

Cheers,
Gazler.

EDIT| Seems to be working on my localhost. Could it be something to do with the domain being an add-on domain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离不开的别离 2024-09-02 01:20:10

在这里试试这个:

$(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;

  });

 });

Here try this :

$(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;

  });

 });
权谋诡计 2024-09-02 01:20:10

我的猜测是,当你删除 false 时,你的常规提交就会发生——在这两种情况下它根本不起作用。

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

My guess is that when you remove the false your regular submit is happening -- it does not work at all in both cases.

I think you need to have the correct values for more of the options. There is a lot of stuff commented out there. Also, it might be better if you just use a button to do the submit and not a form submit, then you don't have to worry about trying to turn off the auto-magic stuff the browser does.

绻影浮沉 2024-09-02 01:20:10

添加自己的解决方案,

iframe 选项需要是:

iframe: true

解决了问题。

感谢大家的意见。

Adding own solution,

The iframe option needed to be:

iframe: true

Which solved the problem.

Thanks to everyone for their input.

相守太难 2024-09-02 01:20:10

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

There will be some fields inside form that is validated and it may be hide so please check the all fields and set its validation it will work for me its work when i saw there was a hidden fields that is hide due to jQuery hide show. Or you can remove all validation from form it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文