[英]Retrieve a value from a form text field and set that value in another form text field using RJS
[英]RJS : Retrieve the value of a text_field
我正在使用Rails 2.3,但我不知道如何檢索表單的text_field的值以便在rjs文件中處理它。 我整天在互聯網上搜索,但沒有找到解決方案。
這是我的代碼更具體:
在我的form.erb文件中:
<%= observe_field 'issue_'+@issue.id.to_s+'_estimated_hours',
:url=>{:action => 'check_estimated_hours_field'},
:with => "'number=' + escape(value) +
'&fields_estimated_ids=#{@fields_estimated_ids}'"
%>
控制器:
def check_estimated_hours_field
@fields_estimated_ids = params[:fields_estimated_ids]
end
check_estimated_hours_field.rjs:
for @field_id in @fields_estimated_ids
# here I want to add all the values of fields and retrieve the result in a ruby variable
end
如何解決在check_estimated_hours_field.rjs的注釋中遇到的問題?
經過多次測試和研究,使用RJS恢復表單字段的值顯然是不可能的。 我發現的解決方案是創建一個包含observe_field中所有字段值的字符串,並將此字符串解析為RJS以檢索值,如下所示:
在我的form.erb文件中:
<% = observe_field 'issue_' + @issue.id.to_s +'_estimated_hours'
: url => {: action => 'check_estimated_hours_field'},
: with => "'number =' + escape (value) +
'& fields_estimated_values =' + $ ('my_form'). select ('[class ~ = estimated_hours]'). collect (function (n) {
return n.value + '_';
}) + "
%>
在控制器中:
def check_estimated_hours_field
estimated_hours_values = params [:estimated_hours_values].split('_')
@total_estimated_hours = 0
estimated_hours_values.each {| value |
@total_estimated_hours += hours.to_f
}
end
這樣我可以添加所有值並在變量@total_estimated_hours中檢索它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.