简体   繁体   English

辅助JS表单提交电话

[英]Secondary JS Form Submission Call

I have a primary JS that posts a code from a form on the home page, then the user gets a secondary form to update details and they hit update on a second form. 我有一个主JS,该JS从主页上的表单发布代码,然后用户获得了辅助表单来更新详细信息,然后他们在第二个表单上命中了update。

Everything works smoothly until the second selection of Submit (known as Update button on my site). 在第二次选择“提交”(在我的网站上称为“更新”按钮)之前,一切都会顺利进行。

Here is the update page: 这是更新页面:

<div class="welcomersvp"><h2>Welcome <?php echo $PrimGuestSalutation; ?>!</h2></div><br>

        <form name="updatersvpform" id="updatersvpform">

///Skipped form data to be concise ///简洁的表格数据

<div class="rsvp-button text-center"><button type="submit" class="hvr-sweep-to-right">Update</button>

And here is the JS handler: 这是JS处理程序:

(function ($) {
    'use strict';
    $("#updatersvpform").submit(function (event) {

        event.preventDefault();

        $("#updateloading").css("display", "inline-block");
        $.post("updatersvp.php", {
                updateaddress1: $("#updateaddress1").val()
            })
            .done(function (data) {
                if (data) {
                    $("#updateloading").hide();
                    $("#UpdateSuccess").slideDown("slow");
                    setTimeout(function () {
                        $("#FullField").slideUp("slow");
                    }, 1500);
                    setTimeout(function () {
                        $("#FullField").html(data);
                    }, 2300);
                    setTimeout(function () {
                        $("#FullField").slideDown("slow");
                    }, 3500);

                    $("#updatersvpform")[0].reset();
                }
                else {
                    $("#updateloading").hide();
                    $("#UpdateError").slideDown("slow");
                    setTimeout(function () {
                        $("#UpdateError").slideUp("slow");
                    }, 1000);
                }
            });

    });
})(jQuery);

When I hit update, it just reloads the primary page with all the variables in the actual address bar. 当我点击update时,它只是使用实际地址栏中的所有变量重新加载了主页。 No reference to the php page (which doesn't seem to be receiving anything). 没有引用php页面(似乎没有收到任何东西)。

Without the rest of the code shown in your question, I am going to assume that the second form is loaded in as a DOM object and that the javascript for its submission was already loaded beforehand. 如果没有问题中显示的其余代码,我将假定第二个表单已作为DOM对象加载,并且其提交的javascript已经预先加载。

If this is the case then change 如果是这种情况,请更改

$("#updatersvpform").submit(function (event)

to

$('body').on('submit', '#updatersvpform', function(event)

The difference is that the latter is as a delegation to watch for DOM content changes of the target (#updatersvpform) within the scope of a parent object (body tag). 区别在于后者是作为委托来监视父对象(body标签)范围内目标(#updatersvpform)的DOM内容更改。

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

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