阿贾克斯与形式的Yii 2提交两次 [英] Ajax form submitting twice with Yii 2

查看:217
本文介绍了阿贾克斯与形式的Yii 2提交两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了看四周,没有任何其他类似职位的帮助我。我在建立了一个 AJAX 基于表单的Yii 2 的jQuery 而且似乎它提交表单的两倍。

I've looked around and none of the other similar posts have helped me. I have built an AJAx based form in Yii 2 and jQuery and it seems it submits the form twice.

我的方式:

$form = ActiveForm::begin([
    'id' => 'company_form',
    'ajaxDataType' => 'json',
    'ajaxParam' => 'ajax',
    'enableClientValidation' => false
]);

我的JS code:

My JS code:

$(document).ready(function() {

    /* Processes the company signup request */

    $('#company_form').submit(function() {
        signup('company');
        return false;
    }); 

})

function signup(type) {

    var url;

    // Set file to get results from..

    switch (type) {
        case 'company':
            url = '/site/company-signup';
            break;
        case 'client':
            url = '/site/client-signup';
            break;
    }

    // Set parameters
    var dataObject = $('#company_form').serialize();

    // Run request  

    getAjaxData(url, dataObject, 'POST', 'json')

        .done(function(response) {

            //.........

        })

        .fail(function() {
            //.....
        });

    // End

}

如果不是标准提交由我停止把返回:FALSE; 中的JavaScript code

Shouldn't the standard submit be stopped by me putting the return: false; in the javascript code?

为什么提交两次?

详细信息:然而奇怪的是,这似乎只发生在第一时间;如果我打提交一次只提交一次;但如果我重新加载页面,并点击提交就会再次两次做到这一点。

More Info: However the strange thing is, that only appears to happen the first time; if I hit submit again it only submits once; but if I reload the page and hit submit it will do it twice again.

推荐答案

您可能需要更改code象下面这样:

You may need to change your code like below:

$('#company_form').submit(function(e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    signup('company');
    return false;
}); 

<一个href="http://api.jquery.com/event.stoppropagation/">http://api.jquery.com/event.stoppropagation/

<一个href="http://api.jquery.com/event.stopimmediatepropagation/">http://api.jquery.com/event.stopimmediatepropagation/

这篇关于阿贾克斯与形式的Yii 2提交两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆