Prevent double submits

别说谁变了你拦得住时间么 提交于 2021-02-11 15:54:02

问题


I am using GAE for an app that has various submit href buttons, and use javascript to submit.

I am having a real tough time trying to figure out how to prevent multiple submits or doubl-clicking. I have tried various methods to disable or remove the href with javascript.

But I am thinking if there is maybe a method to prevent this in the backend.

What methods would you recommend I use?


回答1:


Preventing it on the server side is not trivial - a second call may hit a different instance. So you need to deal with sessions. The code will get complex quickly.

I would recommend disabling the button before a call and reenabling it upon a response.




回答2:


You can use a javascript to disable all submit buttons on your page when a form is submitted. Maybe something like this:

document.forms[0].addEventListener('submit', function() {
    var btns = document.querySelectorAll('input[type="submit"]');
    for (var i = 0; i < btns.length; i++) {
        btns[i].disabled = 'disabled';
    }
});

If you need to also disable other elements you can modify the querySelector.



来源:https://stackoverflow.com/questions/23739630/prevent-double-submits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!