dongzhang8680 2011-11-28 23:25
浏览 29
已采纳

如何在ajax发布后启用或触发提交事件

How can I enable or trigger the submit event after ajax post?

For instance I have this form I don't want to submit it yet,

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">

<input type="hidden" name="item_name_1" value="Book 1" />

...

<input type="submit" name="pay" value="Check Out" />

</form>

until this page is called - session_end.php,

unset($_SESSION[SESSION_CART]);
unset($cart);

jquery,

$("input[name=pay]").click(function(){
    $("#form-paypal").submit(function(){

        $.post('re_post.php', {}, function() {

                        // I get eternal loop if I use either of these lines below,
            //$("#form-paypal").trigger('submit');
            //$("#form-paypal").submit();

        });


        return false;
    })
}); 

I will get the eternal loop on this page re_post.php but the form is not submitted.

  • 写回答

2条回答 默认 最新

  • drphfy1198 2011-11-28 23:30
    关注

    You don't really need the $("input[name=pay]").click handler. A simple .submit() event handler registration should be sufficient.

    As far as your infinite loop problem is concerned you could append .data() to the form to track status and avoid the infinite loop:

    $(function() {
        $('#form-paypal').submit(function() {
            if (!$(this).data('valid')) {
                // we are entering here the first time we try to submit
                $.post('re_post.php', { }, function() {
                    // we set the data so that when we trigger the submission we 
                    // don't perform any more AJAX requests and avoid the infinite loop
                    $('#form-paypal').data('valid', true).submit();
                });
                return false;
            }
            return true;
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用