yii2表单ajax,javascript - Ajax form submitting twice with Yii 2 - Stack Overflow

博客作者遇到了一个技术问题,即在Yii2框架中使用jQuery构建的AJAX表单在首次提交时会提交两次。问题只在页面刷新后首次提交时出现,后续提交则正常。作者的JS代码中已经使用了`return false;`来阻止标准提交,但问题依然存在。讨论可能涉及AJAX请求的处理、事件绑定和防止多次提交的策略。
摘要由CSDN通过智能技术生成

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.

My form:

$form = ActiveForm::begin([

'id' => 'company_form',

'ajaxDataType' => 'json',

'ajaxParam' => 'ajax',

'enableClientValidation' => false

]);

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

}

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

Why is it submitting twice?

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值