Rails:在textfield中執行onchange遠程函數時如何從另一個字段獲取值

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


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:

在表單中,我想做一個AJAX驗證,檢查是否有足夠的項目可用。在每個項目旁邊,我有一個文本框,reserver輸入項目數量。在輸入之后,我想進行一次onchange調用,檢查給定日期的項目數量是否可用。因此,我需要傳遞onchange以下參數中調用的遠程函數:item id,item amount(當前文本字段的值)以及上面的datetime_select字段中給出的拾取時間和返回時間。這是我的代碼:

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

顯然,這不起作用,因為@ reservation.return_time和@ reservation.pick_up_time尚未設置,因為表單尚未發送。我的問題是:我如何獲得這些價值觀?我相信它應該通過javascript調用完成,但我沒有設法在“with”變量中插入一個javascript調用,或者至少沒有讓它工作。有誰知道我應該怎么做?

1 个解决方案

#1


use prototype selectors $(#reservations_pick_up_time).value

使用原型選擇器$(#reservations_pick_up_time).value

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

::with屬性只是你編寫內聯顯示的JS代碼的地方


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/07/15/72fbff0a10618a444292a44af9f9ad22.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com