Rails 4: Disable submit button after click

前端 未结 5 2067
天涯浪人
天涯浪人 2020-12-15 21:54

I have a form_tag(foo_path(@foo), remote: true, id: \'foo-form\' form and a submit button submit_tag (\"Submit\", :id => \"foo-submit\")

<
相关标签:
5条回答
  • 2020-12-15 22:25

    You use CoffeeScript to do something like these lines:

    jQuery ->
      $('.theform').submit ->
        $('input:submit').attr("disabled", true)
    

    This disables the form submit button when the form with class="theform" is submitted. Depending on your need you adjust this to fit with your class/id for the form.

    0 讨论(0)
  • 2020-12-15 22:26

    An alternative to the submit_tag is the button_tag. The button_tag allows you to add a font awesome spinner.

    <%= button_tag 'Submit', class: 'btn btn-primary', 
          data: { disable_with: "<i class='fa fa-refresh fa-spin'>
          </i> Submitting Changes..."} %>
    
    0 讨论(0)
  • 2020-12-15 22:32

    use

    submit_tag "Submit", id: "foo-submit", data: { disable_with: "Please wait..." }
    
    0 讨论(0)
  • 2020-12-15 22:36

    The problem was caused by invalid HTML code. See <div> breaks <form> disable_with works now

    0 讨论(0)
  • 2020-12-15 22:36

    Since it is an ajax call, the resultant js.erb file from the corresponding action will be triggered.

    The disable-on-click algorithm can be placed inside this js.erb file.

    # in the controller
    def foo_method
    ...
    # will render js.erb for ajax call 
    # assuming there is no .html view file in the directory
    end
    
    # in foo_method.js.erb
    $('input:submit').attr("disabled", true);
    

    It should bring you abck to the same page with the button noew disabled

    0 讨论(0)
提交回复
热议问题