jquery - 在呈现表单之前提交表单

时间:2013-05-06 10:36:23

标签: javascript jquery ajax

我正在使用此代码:

$(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”。我该怎么办?

1 个答案:

答案 0 :(得分:0)

使用.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;

                });


            });
        }
    });
});