简体   繁体   中英

How do I check if a form is being submitted with a new value?

I have a simple form. Now, what I need to do is hide a div element if the form is being submitted with a new value which is not the same as the one searched the first time.

how do i do it?

I am using jquery and ruby on rails

Here is the code

<%= form_tag home_path, :method => "post", :name=>"onLoad",  :remote=> true  do %>
                <div class="containePrepend">
                    <% if params[:searchedValue].nil? == true%>

                    <%=text_field_tag 'search', nil, :placeholder => 'Please enter the product to continue' %>
                    <%else%>
                    <%=text_field_tag 'search', nil, :value => params[:searchedValue]%>
                    <%end%>

                </div>
                <%= button_tag(:type => 'submit', :id => "buttonSubmit") do %>
                <% end %>
                <% end %>

The form is being submitted via ajax, which appends to the div

<div class="product_first_roll"></div>
<div class="product_old_roll"></div>

Now, if the value is searched is newer than the previous one. I want to hide contents from the old search inside

<div class="product_first_roll"></div>
<div class="product_old_roll"></div>

And only show the new one inside

<div class="product_first_roll"></div>
<div class="product_old_roll"></div>

Sorry if it sounds too complicated or messed up. If i just know how to check if a form is being submitted with a new value, i can figure the rest out.

Thanks

One way is:

Just have a single div and keep on updating the same div with new search results after every search, so that there no need of hiding the old div.

Alternatively,

Add an hidden field in the form like <%= hidden_field_tag :previous_search_value, params[:searchValue] %> and now in the controller action ,

@hidden = params[:previous_search_value].eql?(params[:searchValue])

and now after this action done, in the success function or in .js.erb file add the following logic to hide your div.

<% if @hidden %>
  $("#product_old_roll").hide();
<% end %>

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM