我已经看了看四周,没有任何其他类似职位的帮助我。我在建立了一个
和 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;
});
http://api.jquery.com/event.stoppropagation/
http://api.jquery.com/event.stopimmediatepropagation/