2015-07-01 25 views
0

問題是我有一個JavaScript,使我的用戶可以設置一個值可以說'0.6',並設置此值,他們可以將其複製到我在JavaScript中指定的類I​​D。問題是我想找出一種方法來限制這個,所以可以說他們複製了值,但只希望它粘貼到前5個文本字段中,我怎麼才能設置限制粘貼值的次數,我很困惑如何做到這一點,因爲我的類ID可以有任何數量的文本字段。任何幫助將不勝感激!!!如何限制正在複製的某個值的數量。

這裏是我的JavaScript複印功能

$('.val_class').click(function() { 
    var txt; 
    var r = confirm("Are you sure you want to copy this value to each sample?."); 
    var text = $('.val_box').val(); 
    if (r == true) { 
     $('.reading_class').val(text); 
    } else { 

    } 
    }); 

這是我的看法。

%th 
    %h2 Set a value to be copied to each sample. 
    %br 
    %tbody 
    %td 
     .js 
     %li Reading One 
     %br 
     =f.text_field :valc, :id => 'valc', :class => 'val_box', :hint => "You can set a value in this text box that will be copied to all of the samples, by pressing copy value button." 
     .control-group 
     .controls 
      %button{:type => "button", :class => 'val_class', :id => "val_class"}Copy this value to all samples(Reading One) 


[email protected] do |sample| 

=number_field_tag "dimension[samples][#{sample.id}][value1]", sample.value_for_dim(@dimension, index+1), :id => 'reading', :class => 'reading_class', :step => "0.000001" 

回答

1

比方說你有一個選擇$('.my_class'),並希望把他們的只有第一5,簡單地做:

$('.my_class').slice(0, 5).val(text) 

更多信息上.slice()here

+0

完全忘記了片太感謝你了! @Piotr Kruczek – Snowman