简体   繁体   English

AJAX:提交没有页面刷新的表单只能运行一次

[英]AJAX: Submitting form without page refresh only works once

I am using AJAX to submit a form behind the scenes, without refreshing the page. 我正在使用AJAX在幕后提交表单,而不刷新页面。 The problem I am running into is I can only submit the form once. 我遇到的问题是我只能提交一次表格。 After I submit it once, the on('submit') function no longer works and I am getting no errors. 提交一次后,on('submit')函数不再有效,我没有错误。 This completely defeats the purpose of using AJAX to submit the form :/ 这完全违背了使用AJAX提交表单的目的:/

           $(document).on('submit', '#myForm', function(e) {
                $.post('mail.php', $(this).serialize(), function (data) {
                    //SUCCESS
                    $('.successORfail').html(data);
                     setTimeout(function(){
                      $(".successORfail").fadeOut("slow", function () {
                        $(".successORfail").remove();
                      });
                    }, 4500);

                }).error(function() {
                    alert("Fatal Error: mail.php not found!");
                });
                e.preventDefault();
            });

I was wondering if someone ran into a similar problem or knows how to solve this? 我想知道是否有人遇到类似问题或知道如何解决这个问题? I would like to be able to submit the form more than once, making changes to the form input values after each submit, if needed. 我希望能够多次提交表单,如果需要,在每次提交后更改表单输入值。

Many thanks in advance 提前谢谢了

Are you sure the AJAX request is not happening? 你确定AJAX请求没有发生吗? It looks like you are removing the .successORfail element from the page, and thus the there is nothing to append the content to on subsequent calls. 看起来您正在从页面中删除.successORfail元素,因此没有任何内容可以在后续调用中附加内容。

Check your console and you will probably notice an ajax call happening each time. 检查你的控制台,你可能会注意到每次都会发生ajax调用。

Try changing your setTimeout to this: 尝试将setTimeout更改为:

var msgEl = $(".successORfail");
setTimeout(function() {
    msgEl.fadeOut("slow", function () {
        msgEl.empty().show();
    });
}, 4500);

Your success event handler: 您的成功事件处理程序

$('.successORfail').html(data);
setTimeout(function () {
  $(".successORfail").fadeOut("slow", function () {
    $(".successORfail").remove();
  });
}, 4500);

is setting content in an element ( .successORfail ), then removing that element. 在元素( .successORfail )中设置内容,然后删除该元素。 The next time you submit the form, get a successful response, and that function is executed the element is no longer there to set the content into so you wouldn't see anything change. 下次提交表单时,获得一个成功的响应,并且该函数被执行,元素不再存在以设置内容,因此您不会看到任何更改。

Instead of removing the element, just .hide() it so that the next time it can be populated. 而不是删除元素,只需.hide() ,以便下次可以填充它。 You'll need to .show() it each time too. 你每次都需要.show()它。

       $(document).on('submit', '#myForm', function(e) {
            $.post('mail.php', $(this).serialize(), function (data) {
                //SUCCESS
                $('.successORfail').html(data).show(); //<-- show

                 setTimeout(function(){
                  $(".successORfail").fadeOut("slow", function () {
                    $(this).hide(); //<-- hide
                  });
                }, 4500);

            }).error(function() {
                alert("Fatal Error: mail.php not found!");
            });
            e.preventDefault();
        });

Also in the fadeOut() function, you can access the element with $(this) instead of re-selecting it based on the class name. 同样在fadeOut()函数中,您可以使用$(this)访问元素,而不是根据类名重新选择它。

Can you add some HTML-snippet? 你能添加一些HTML片段吗? Its hard to help without knowledge about your html-structure, because if you are replacing the form via $('.successORfail').html(data); 没有关于你的html结构的知识很难帮助,因为如果你通过$('。successORfail')替换表单.html(data); the listener isn't re-bound to the form. 监听器不会重新绑定到表单。 You should also return FALSE because the form-data is sent via javascript. 您还应返回FALSE,因为表单数据是通过javascript发送的。

Well, it seems that you append your result to $('.successORfail').html(data); 好吧,似乎你将结果附加到$('.successORfail').html(data); and the remove it. 并删除它。 Take out the following and it should work multiple times... 取出以下内容,它应该多次工作......

$('.successORfail').remove();

Without that element, the change can't be made. 没有该元素,就无法进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM