Rails 3: submit Remote form via jQuery - prevent page from reloading

空扰寡人 提交于 2019-12-08 12:46:35

问题


Here how my rails form looks like:

  - form_tag rel_references, :remote => true, :id=>"references_form", :method => 'get' do               
    = text_field_tag :rel_id, params[:rel_id], :id=>"reference_form_rel_id"
    = submit_tag "Get references", :id => "referencesubmit"

I need the textfield #reference_form_rel_id to be dynamically populated based on the HTML of a DOM element using jQuery and then submit this form (remotely) from within the same jQuery function. So this is what I am doing:

  $(".classname").live('click',function(){  
     var relationship_id = $('#id_store').html().trim();
     $('#reference_form_rel_id').val(relationship_id);  // populate the text field
     $('#references_form').submit();       // submit the form

});

Now, the form does get submitted, however, it is not submitted remotely, and the page reloads thus defeating the purpose of the :remote => true. Please guide me on how to correctly submit a rails remote form via jQuery. Thanks!


回答1:


you may try

$('#referencesubmit').trigger('click')


来源:https://stackoverflow.com/questions/9027282/rails-3-submit-remote-form-via-jquery-prevent-page-from-reloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!