.

Auto disabling the submit button with KRJS

Benjamin over at RubyOnRailsBlog was recently wondering about how to painlessly disable a submit button to prevent the user from hitting it multiple times.

This requires some sort of javascript hackery to achieve, but the easiest way I’ve found to do this is with my trusty old KRJS pocketknife.

  1. # in application controller
  2.  
  3. def on_commit_click
  4.   render :update |page| do
  5.    page[‘commit’].disabled = true
  6.    page << "$(’commit’).form.submit();"
  7.   end
  8. end
  9.  
  10. # in the form
  11.  
  12. <%= submit_tag ‘Submit’, :id=>’commit’ %>

This will add this behavior for all submit buttons with the ‘commit’ id. The hardest part here is remembering to add the ‘commit’ id.

Requires: KRJS Plugin

4 Responses to “Auto disabling the submit button with KRJS”

  1. October 9th, 2006 | 2:00 am

    Great idea of using krjs to achieve this globally. Now, how do we remove the disabled aspect once the commit / post has completed?

  2. October 9th, 2006 | 10:23 am

    If you are using a remote form, you can add an ‘after’ callback that calls a script like “$(’commit’).disabled = false”. This will fire off when the AJAX action returns.

    You could also add a similar line at the end of an RJS template render to turn it back on.

  3. August 7th, 2009 | 7:56 pm

    ???? ??????! ? ????? ??????? ? ?????? ??????? ?????? ? ??????? ???????? ???????????. ??? ?? ????? ?????? ????????????? ????? ?????????????? ????????? ????? midland. ????? ?? ?????? ?? ???? ? ?? ?? ????? midlands.ru ?????? ?? ???? ?? ???????. ???-?????? ??????????? ??? ?????? ??? ??? ?? ?????????? ?

  4. August 9th, 2009 | 8:15 pm

    Good afternoon
    Thus using the name and the shape of the brand pills a manufacturer producing generic medications will be accused of the copyright infringement. cheap online
    Pa!!!
    ________________________________
    where to buy in the united kingdom :)

Leave a reply