Clear Rails remote form after submitting

前端 未结 5 1786
刺人心
刺人心 2020-12-31 08:51

I\'m using Rails 3 with jQuery, and have a simple form with a single text input and a submit button. I have the form submitting with :remote => true, and wou

相关标签:
5条回答
  • 2020-12-31 09:29

    In your model.js file

                   $('#element_id').val(' ');
    

    It work's for me, you can try this.

    0 讨论(0)
  • 2020-12-31 09:30

    In your action.js.erb file

    $('#message_content').attr('value','');
    

    Where action is the method name (which you don't state in your question) in your controller

    0 讨论(0)
  • 2020-12-31 09:39

    You can trigger a Rails AJAX submit like this:

    $(this).trigger('submit.rails');
    
    0 讨论(0)
  • 2020-12-31 09:44

    When submit button is pressed for a form marked remote => true, javascript code in rails.js does the submit(). You need not override the functionality.

    Try this instead in $(document).ready -

    (Edit: Using bind instead of live. Thanks Darren Hicks.)

    $("#new_message").bind("ajax:complete", function(event,xhr,status){
      $('#message_content').val('');
    }
    

    This adds an event handler for the ajax:complete event. This event is fired when your the ajax-submit-form is completed. More here.

    Edit:

    Don't forget closing ) on the bind method.

    $("#new_message").bind("ajax:complete", function(event,xhr,status){
       $('#message_content').val('');
     })
    
    0 讨论(0)
  • 2020-12-31 09:44

    In your jquery function, do the submitting yourself via get(). You set the text field to blank right after calling get(). Or, you can do this in the success callback that you provide to get(), which will let you get some info back from your controller, like whether the submitted text field value was acceptable.

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