How to disable a submit button after submission in PHP?

前端 未结 8 2097
耶瑟儿~
耶瑟儿~ 2021-01-27 08:21

I browsed through the other questions on this issue but couldn\'t find a satisfactory answer. I have a quiz website where the user selects an option from four options and then c

8条回答
  •  时光取名叫无心
    2021-01-27 08:47

    Available Solutions

    Since you tagged the question jQuery, here are some simple jQuery solutions you can use:
    To disable it when it is clicked, you can simply:

    $('input[type="submit"]').click(function() {
        this.disabled = true;
    };
    

    You can do even better though, and disable it only once the form is submitted:

    $("form").submit(function() {
        $(this).find('input[type="submit"]').prop("disabled", true);
    });
    

    Solution Demo

    Here's a simple demo of the above logic, in a jsFiddle: http://jsfiddle.net/zb8CZ/
    (Note that in that implementation I didn't use jQuery, but rather, plain JS; for portability across browsers however, as well as access to the many other methods jQuery provides (plus the syntactic sugar!) I recommend the jQuery methods.

    Good to note...

    Note that attempting to implement JS event handling inline in your HTML (using the onsubmit="..." or onclick="..." attributes) is considered bad practice, since it muddles your functionality with your layout/display layer. You also lose any syntax highlighting and/or error-checking your editor might provide, as well as just generally making it harder to develop and maintain your application, since there is no logical order to your code.

提交回复
热议问题