RJS:检索text_field的值

RJS : Retrieve the value of a text_field

本文关键字:field 的值 text 检索 RJS      更新时间:2023-09-26

我使用的是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.js:

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 中检索它们