如何将text_field_tag的值复制到另一个text_field_tag

时间:2015-04-17 17:29:07

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

我想将此文本字段标记的值复制到另一个text_field标记。我尝试过这个,但它没有用。

这是我的观点:

      -if @dimension.style =~ /visual/i
        .control-group.radio_buttons.custom_radio
          .controls
            = content_tag :label, :name => "sample_dimensions_#{sample.id}_value_1", :class => 'radio custom_radio' do
              = radio_button_tag "dimension[samples][#{sample.id}][value1]", 1, sample.value_for_dim(@dimension, index+1) == 1, :class => 'radio_button'
              Pass
            = content_tag :label, :name => "sample_dimensions_#{sample.id}_value_0", :class => 'radio custom_radio' do
              = radio_button_tag "dimension[samples][#{sample.id}][value1]", 0, sample.value_for_dim(@dimension, index+1) == 0
              Fail
      -else

        #I tried giving it an id but still isnt working
        =text_field_tag "dimension[samples][#{sample.id}][value1]", sample.value_for_dim(@dimension, index+1), :id => 'reading1' , :step => "0.000001"


    -when 1
      -if @dimension.style =~ /visual/i
        .control-group.radio_buttons.custom_radio
          .controls
            = content_tag :label, :name => "sample_dimensions_#{sample.id}_value_1", :class => 'radio custom_radio' do
              = radio_button_tag "dimension[samples][#{sample.id}][value2]", 1, sample.value_for_dim(@dimension, index+1) == 1, :class => 'radio_button'
              Pass
            = content_tag :label, :name => "sample_dimensions_#{sample.id}_value_0", :class => 'radio custom_radio' do
              = radio_button_tag "dimension[samples][#{sample.id}][value2]", 0, sample.value_for_dim(@dimension, index+1) == 0
              Fail
      -else

        #I tried giving it an id but still isnt working
        =text_field_tag "dimension[samples][#{sample.id}][value2]", sample.value_for_dim(@dimension, index+1), :id => 'reading2', :step => "0.000001"

以下是我的JavaScript,其函数是将reading1的值复制到reading2

$("#reading1").change(function() {
  var text = $("#reading1").val();
  $("#reading2").val(text);
});

1 个答案:

答案 0 :(得分:0)

change需要被投入on,如此:

$("#reading1").on('change', function() {
  var text = $("#reading1").val();
  $("#reading2").val(text);
});