drh96824 2015-05-29 15:14
浏览 59
已采纳

jquery / ajax - 单击任何表单上的“提交”按钮3秒钟

I have searched and searched and found nothing that explains what I need.

I have a webscript that has dozens of forms all over the place. I need to disable the submit button for 3 seconds after submit is clicked on any form no matter what the id, class, or action of the form.

I found the following code:

$('#btn').click(function(){
    var btn = $(this);
    $.post('http://someurl.com',{delay: 3}).complete(function(){
btn.prop('disabled', false);
    });
btn.prop('disabled', true);

});

but this requires the form have an id of btn and it appears to require the form be "posted" to some page.

Is there a way to achieve this on all forms reguardless of the action being performed? So if its a signup form, it disables the "join" button for 3 seconds, if its a comment form, it equally disables the submit for 3 seconds after submitting comment, .... basically no matter what the form or action I want to disable the submit button for 3 seconds after click.

Is this even possible?

I have tried changing all instances of "#btn" to "input" but this doesn't seem to work. I am really not great at this stuff but seem to manage stumbling through things with the help of others.

The reason my question is NOT a duplicate of other questions is because I specifically stated "no matter what the id, class, or action of the form". The solutions presented on the other question do not specifically address this. Adding id's, classes, or the likes is not a remote possibility on a webscript with hundreds and hundreds of forms for various reasons (its a social networking script). The solution I marked as correct is the solution that did not require any attributes to be added to the html elements.

  • 写回答

2条回答 默认 最新

  • doujiang2643 2015-05-29 15:19
    关注

    Try this.(pseudo-code, modify as needed):

    $('#mybutton').on('click', function() {
        $('[type="submit"]').prop('disabled', true);
        setTimeout(function() {
            $('[type="submit"]').prop('disabled', false);
        }, 3000); // 3 seconds 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?