2016-10-24 2 views
0

テキストフィールドの値を別のフィールドに追加する関数を作成したいのですが、ここでの問題はRuby on Railsのフォームを使用していて、フィールドからIDを取得できるかどうかわかりません私は処理したい。 ここに私のコードです:Ruby on Railsのform_for要素をJavascript関数に渡すにはどうすればいいですか?

これはネストされたフォームです。

function myFunction(text) { 
    var amount = parseFloat($('#purchase_order_po_amount').val()); 
    var amount1 = document.getElementById(text).value; 
    alert(amount1+amount); 
} 

答えて

0

あなたがしたいフィールドを表しますthisを渡すことができます。

<%= f.fields_for :invoices do |invoice_form| %> 
    <br> 
    <%= invoice_form.label :in_number %> 
    <br> 
    <%= invoice_form.text_field :in_number %> 
    <br> 
    <%= invoice_form.label :in_amount %> 
    <br> 
    <%= invoice_form.text_field(:in_amount,:onkeypress => "myFunction(document.getElementById('in_amount'))") %> 
    <br> 
    <%= invoice_form.label :in_date %> 
    <br> 
    <%= invoice_form.date_select :in_date %> 
    <br> 
    <%= invoice_form.label :in_meshHr %> 
    <br> 
    <%= invoice_form.text_field :in_meshHr %> 
    <% end %> 

そして、ここでは私のjavascript関数です。

<%= invoice_form.text_field(:in_amount,:onkeypress => "myFunction(this)") %> 

、その後、あなたの関数であなたが行うことができます。

function myFunction(element) { 
    var amount = parseFloat($('#purchase_order_po_amount').val()); 
    var amount1 = $(element).val(); 
    alert(amount1+amount); 
} 
関連する問題