在呈现表单之前提交表单

on submit form before form is rendered

本文关键字:表单 前提 提交      更新时间:2023-09-26

我正在使用这个代码:

$(document).ready(function () {
$("#step1_next").validate({
    submitHandler: function(form) {
        $.post('step2.php', $("#step1_next").serialize(), function(data) {
            $('#step2_div').html(data);

            $("#step2_form").on("submit", function() {
                $.post('../model/step2.php', $("#step2_form").serialize(), function(data) {
                    $('#step2_controller').html(data);
                });
                return false;
            });

        });
    }
});
});

基本上,我想做的是,当step2.php被渲染时,另一个ajax函数附加到step2_form(在step2.php中)。

这段代码不能工作可能是因为"step2_form"还没有加载。我能做什么?

使用.on()使用事件委托

$(document).ready(function () {
    $("#step1_next").validate({
        submitHandler: function(form) {
            $.post('step2.php', $("#step1_next").serialize(), function(data) {
                $('#step2_div').html(data);

                $(document).on("submit", "#step2_form", function() {
                    $.post('../model/step2.php', $("#step2_form").serialize(), function(data) {
                        $('#step2_controller').html(data);
                    });
                    return false;
                });

            });
        }
    });
});