问题
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