Rails 3 Collection选择onchange提交 [英] Rails 3 Collection Select onchange Submit

查看:95
本文介绍了Rails 3 Collection选择onchange提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当收藏集选择"的值更改时,我正在尝试提交远程表单.我尝试过:

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 %>"

但是我知道remote_function不再位于rails 3中.所以我也尝试过:

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

{:onchange => "submit();"

但是什么也没发生.

推荐答案

您需要为此使用Javacsript.如果您使用的是jQuery,这很简单.您将需要侦听'change'事件并在激发时提交表单.我使用此通用函数,然后使用"submit_on_change"类标记要在更改时自动提交的表单:

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;
    });
  });
});

确保在dom加载后运行此文件,例如在准备好文档时运行.

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

这篇关于Rails 3 Collection选择onchange提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆