Rails: how to get value from another field when executing an onchange remote function in textfield

扶醉桌前 提交于 2019-12-06 15:49:42

问题


I want to create a form for making a reservation for borrowed items. A reservation consists of pick up time, return time and items the reserver wants to borrow and their amounts. For example, I might want to reserve three plates, three knives and three forks for the rest of the week.

In the form, I want to do an AJAX validation that checks whether there is enough items available. Next to each item I have a text box with which the reserver inputs the item amount. After the input, I want to do an onchange call that checks if the amount of items is available for the given dates. Thus, I need to pass the remote function called in the onchange following parameters: item id, item amount (value of the current textfield) and pick up time and return time which are both given in datetime_select fields above. This is my code:

<% with = "'amount='+value+'&item=#{item.id.to_s}&pick_up_time=#{@reservation.pick_up_time.to_s}&return_time=#{@reservation.return_time.to_s}'" %>
<%= text_field_tag "reservation[#{prefix}reserved_items][#{item.id}]", get_amount_value(item, @reservation), :size => 3, :onchange => "#{remote_function(:url => { :controller => :items, :action => :availability }, :with => with, :update => availability_url) }" %> 

Obviously, this does not work since @reservation.return_time and @reservation.pick_up_time are not yet set because the form is not yet sent. My question is: how do I get those values? I believe that it should be done via a javascript call, but I didn't manage to insert a javascript call in the "with" variable or at least didn't get it to work. Does anybody have any idea on what I should do?


回答1:


use prototype selectors $(#reservations_pick_up_time).value

the :with attribute is just a place for you to write JS code that it will display inline



来源:https://stackoverflow.com/questions/1131694/rails-how-to-get-value-from-another-field-when-executing-an-onchange-remote-fun

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