How can I prevent a double submit with jQuery or Javascript?

前端 未结 10 1271
终归单人心
终归单人心 2020-12-08 22:44

I keep getting duplicate entries in my database because of impatient users clicking the submit button multiple times.

I googled and googled and found a few scripts,

10条回答
  •  -上瘾入骨i
    2020-12-08 23:02

    I'm not sure what language/framework you're working with or if it's just straight HTML. But in a Rails app I wrote I pass a data attribute on the form button disable_with which keeps the button from being clickable more than once while the transaction is in process.

    Here's what the ERB looks like.

    <%= f.button "Log In", class: 'btn btn-large btn-block btn-primary', data: {disable_with: "Logging In..."} %>
    

提交回复
热议问题