阿贾克斯窗体不能与事件preventDefault prevent页面重载()。窗体、能与、事件、页面

2023-09-10 22:15:48 作者:走路太酷↘会掉内裤

我有以下的code应该通过Ajax提交表单,而无需重新加载页面,该页面:

I have the following code which is supposed submit a form via Ajax without having to reload the page:

$( document ).on('submit', '.login_form', function( event ){
    event.preventDefault();
    var $this = $(this);
    $.ajax({
        data: "action=login_submit&" + $this.serialize(),
        type: "POST",
        url: _ajax_login_settings.ajaxurl,
        success: function( msg ){

            ajax_login_register_show_message( $this, msg );

        }
    });
});

但出于某种原因,尽管事件preventDefault(); 功能,从实际发射应该prevent的形式,它实际上火。

However for some reason, despite the event.preventDefault(); function which is supposed to prevent the form from actually firing, it actually does fire.

我的问题是,我怎么prevent上述形式的重新加载页面?

My question is, how do I prevent the above form from reloading the page?

感谢

推荐答案

不重视文档上的监听器,而不是提交按钮使用的点击处理程序,并类型更改为按钮。

don't attach a listener on document instead use a on click handler on the submit button and change the type to button.

<button id="form1SubmitBtn">Submit</button>
$('#form1SubmitBtn').click(function(){

  //do ajax here
});

编码快乐!

 
精彩推荐
图片推荐