How to Prevent Users from Submitting a Form Twice

后端 未结 16 1001
挽巷
挽巷 2020-12-01 06:59

when user click add button twice, from get submitted twice with same dat

16条回答
  •  孤城傲影
    2020-12-01 07:23

    Put a class on all your buttons type="submit" like for example "button-disable-onsubmit" and use jQuery script like the following:

    $(function(){
        $(".button-disable-onsubmit").click(function(){
             $(this).attr("disabled", "disabled");
             $(this).closest("form").submit();
        });
    });
    

    Remember to keep this code on a generic javascript file so you can use it in many pages. Like this, it becomes an elegant and easy-to-reuse solution. Additionally you can even add another line to change the text value as well:

    $(this).val("Sending, please wait.");
    

提交回复
热议问题