javascript - 从表单文本字段检索值并使用 RJS 在另一个表单文本字段中设置该值

标签 javascript ruby-on-rails ruby ajax ruby-on-rails-3

我正在使用 Ruby on Rails 3,我想从表单文本字段检索一个值,并使用 RJS 在另一个表单文本字段中设置该值,如下所示(或以更好的方式!):

update_page_tag do |page|
  page.event.observe('text_field1_css_id', 'keyup') do |element|
    element << ... # Get and set a '@variable' value using the value in the 
                   # HTML textarea tag with id = 'text_field1_css_id'. The 
                   # '@variable' value should be accessible from the
                   # 'element.replace_html' method stated below
    element << ...
    ...
    element.replace_html (:text_field2_css_id), @variable.inspect
  end
end

如何使用Prototype框架做到这一点?

最佳答案

在原型(prototype)中:

// /public/application.js
$('text_field_1_css_id').observe('keyup',function(event){
   $('text_field2_css_id').setValue($F(event.element));
});
<小时/>

对于其他可能感兴趣的人,如果您使用 jquery,您可能可以使用:

// /public/application.js
$('#text_field_1_css_id').keyup(function(){
   $('#text_field2_css_id').value($(this).value());
});

关于javascript - 从表单文本字段检索值并使用 RJS 在另一个表单文本字段中设置该值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6267088/

相关文章:

javascript - 将图像与文本叠加、保存并共享

ruby - 使用ARGF时如何使用STDIN?

javascript - 在 Bootstrap 折叠导航栏中有不同的内容

javascript - 带键的 ReactJS 动态子项

ruby-on-rails - 如何使用 ActiveAdmin 或 RailsAdmin 保持 DRY,与主应用程序分开

MySQL数据类型SET与rails

ruby - ruby 混淆中的正则表达式匹配

ruby - 尝试运行新的 bundle gem 时出现 Bundler 错误?未定义的方法 `prefer_gems_rb?'

javascript - 更改对象而不在 JavaScript 中引用它

php - jquery 帮助获取表行的 Id