RJS:检索text_field的值
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.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 中检索它们
相关文章:
- 如何使用jquery在填充自动完成的值后使文本框只读
- 如何使用jQuery选择下拉列表的值
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 如何在php文件中获取$.post-ajax传递的值
- Javascript计数器变量未显示正确的值
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 接受不在列表中的值-引导组合框
- 提交后保留下拉选择的值
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 使用元素的值与innerHTML
- 获取HTML属性中CSS声明的值
- 如何在 Javascript 中将变量的值从字符串转换为整数
- Lodash 从值数组中查找数组中的值
- 从键值结构中获取数据,并将其与AngularJS中ng重复的值进行比较
- 将数组中的值插入到表中
- 使用Javascript使用数组检查文本框中的值
- 无法使用PHP动态设置下拉列表中的值
- 对具有ui typeahead的对象中的值执行orderBy
- 如何在单击按钮时返回Input Field的值?