Tony
Tony

Reputation: 13

Rails 4 - Javascript onchange issue

Rails 4 - collection_select JS issue.

I am trying to get form to submit when a dropdown value is changed. Ive tried several different solutions, but the JS never fires for in my collection_select. I can get it to fire for an input without an issue, just not a collection_select.

Here is my current code

 <%= search_form_for @search do |f| %>
   <%= f.collection_select(:assign_eq, @users.all, :name, :name, :prompt => true, :prompt => 'Sort By Account Manager', :onchange => "this.form.submit()")%>
<% end %>

Upvotes: 1

Views: 1214

Answers (2)

Sumit Munot
Sumit Munot

Reputation: 3868

You can check here to get on change event by writing a javascript/jQuery on form submit.

Reference: Rails 3 Collection Select onchange Submit

Under document ready:

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

Upvotes: 0

sites
sites

Reputation: 21815

From here, you note that method signature requires a second hash:

<%= f.collection_select(:assign_eq, @users.all, :name, :name, {:prompt => 'Sort By Account Manager'}, :onchange => "this.form.submit()")%>

i.e. enclose prompt with braces, to denote the second hash which is meant to contain html options.

I also deleted one prompt option.

Upvotes: 2

Related Questions