爲了處理表單我使用下面的代碼(僅用於測試):如何防止在Yii2中提交多個表單?
$(document).on("beforeSubmit", "#test-form", function (event, messages) {
$(this).find(':submit').attr('disabled', true);
console.log('Test new form');
return false;
});
但是,儘管我做一個提交按鈕爲不活動,我們可以在控制檯中看到,這種形式是當我快速點擊按鈕時,至少提交兩次。作爲一個臨時修復,寫了下面的代碼:
$(document).on("beforeValidate", "form", function(event, messages, deferreds) {
$(this).find(':submit').attr('disabled', true);
console.log('BEFORE VALIDATE TEST');
}).on("afterValidate", "form", function(event, messages, errorAttributes) {
console.log('AFTER VALIDATE TEST');
if (errorAttributes.length > 0) {
$(this).find(':submit').attr('disabled', false);
}
});
$(document).on("beforeSubmit", "#test-form", function (event, messages) {
console.log('Test new form');
return false;
});
但不知道這是一個好的決定與否。如何解決這個問題?
在此先感謝!
有人提出使用隨機令牌的解決方案:https://github.com/yiisoft/yii2/issues/10498 –
@Kostas Mitsarakis絕對是一個很好的服務器側的判斷。如果我理解正確,JS不能給予100%的預防這個問題的保證,對吧? – jekahm
是的,因爲JavaScript可以被禁用或因爲瀏覽器問題。 –