2016-09-18 42 views
0

對不起,我英語不好。如何創建一個字段只有在使用Rails和JavaScript選擇一個選項時纔會出現?

我正在使用Ruby on Railland編寫WebApp,並且只有在選擇了選項時我才需要顯示一個字段。

這裏是我的問題的形式:

應用程序/視圖/ gastos/_form.html.erb

 <div class="field" id="ddlSelect"> 
    <%= f.label "Tipo de Pagamento" %><br> 
    <%= f.select(:payment_type, options_for_select([['À vista', 1], ['À prazo', 2], ['Misto', 3]]), { include_blank: true }) %> 

    <input type="text" id="hdnPro" value="product" style='display:none'/> 
    </div> 

應用程序/資產/ Java腳本/ gastos.js

var ddlSelect; 

ddlSelect = function() { 
    $('#ddlSelect').('change',function(){ 
    if($(this).val() == 'À prazo' || 'Misto'){ 
     $('#hdnPro').show(); 
     }else{ 
     $('#hdnPro').hide(); 
     } 
    }); 

} 

$(document).ready(function() { 
    ddlSelect(); 
}) 

兩個其他問題:如何重寫這行以嵌入紅寶石

<input type="text" id="hdnPro" value="product" style='display:none'/> 

以及如何將_form連接到gastos.js。

謝謝

回答

0

這就是我如何修復我的代碼。

<div> 
       <div class="field"> 
       <%= f.label "Forma de Pagamento" %><br> 
       <%= f.select :forma_pagamento, forma_de_pagamento, { include_blank: true }, { :id => "forma_pagamento"} %> 
       </div> 
       <script > 
       $('#forma_pagamento').on('change',function(){ 
        if($(this).val() == 1){ 
         $('#avista').show(); 
         $('#aprazo').hide(); 
         $('#parcelas').hide(); 

        }if($(this).val() == 2){ 
         $('#avista').hide(); 
         $('#aprazo').show(); 
         $('#parcelas').show(); 

        }if($(this).val() == 3){ 
         $('#avista').show(); 
         $('#aprazo').show(); 
         $('#parcelas').show(); 
        } 
       }); 
       </script> 
相關問題