Rails 3 Collection Select onchange Submit

本秂侑毒 提交于 2019-12-11 07:27:13

问题


I'm trying to submit a remote form when the value of a Collection Select changes. I've tried:

"<%= form_tag ( { :controller => :images, :action => :index, :method => "post" }) do %>"

"<%= collection_select(:admin_image, :category_id, Admin::Category.all, :id, :name, {:onchange => remote_function(:url => {:controller => "/admin/admin_home", :action => "index"}, :with => "'category_id='+value")}) %>"

"<% end %>"

But I understand that remote_function is no longer in rails 3. So I've also tried:

{:onchange => "submit();"

But nothing happens.


回答1:


You'll need to use Javacsript for this. If you're using jQuery, it's simple. You'll want to listen for the 'change' event and submit the form when it fires. I use this generic function and then tag forms I want to auto-submit on change with the "submit_on_change" class:

$("form.submit_on_change").each(function(idx,form){
  $(form).find("select,input").each(function(idx,element){
    $(element).change(function(){
      $(form).submit();
      return false;
    });
  });
});

Make sure you run this after the dom loads, for example on document ready.




回答2:


After searching on the web, I found a better solution. Hope it helps....

"/admin/images", :action => "index" }) do %> "submit();" %>

来源:https://stackoverflow.com/questions/9370046/rails-3-collection-select-onchange-submit

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