2017-02-27 3 views
0

これは私のviewです。いずれかを選択できる3つのラジオボタンがあります。たとえば、Hourlyを選択すると、#send_to_oneが表示されます。Rails:ドロップダウンリスト値提出なし

ドロップダウンリストを表示するとうまくいきますが、ドロップダウンリストの値を送信すると、nilになります。以下は

<div id="send_to"> 
    <div class="field"> 
    <%= f.label :package %><br> 
    <%= f.radio_button :package, 'Hourly' %>Hourly<br/> 
    <%= f.radio_button :package, 'Daily' %>Daily<br/> 
    <%= f.radio_button :package, 'Monthly' %>Monthly<br/> 
    </div> 
    <div id="send_to_one"> 
    <div class="field"> 
     <%= f.label :day %>Hourly<br> 
     <%= f.select(:day, [['1'],['2'],['3'],['4'],['5'],['6'],['7'],['8'],['9'],['10'],['11'],['12'],['13'],['14'],['15'],['16'],['17'],['18'],['18'],['20'],['21'],['22'],['23']],{include_blank: "-select-"}) %> 
    </div> 
    </div> 
    <div id="send_to_two"> 
    <div class="field"> 
     <%= f.label :day %>Daily<br> 
     <%= f.select(:day, [['1'],['2'],['3'],['4'],['5'],['6'],['7'],['8'],['9'],['10'],['11'],['12'],['13'],['14'],['15'],['16'],['17'],['18'],['18'],['20'],['21'],['22'],['23'],['24'],['25'],['26'],['27'],['28'],['29'],['30']],{include_blank: "-select-"}) %> 
    </div> 
    </div> 
</div> 

私のjavascriptのコードです:

$(function() { 
    $("#send_to_one").hide(); 
    $("#send_to_two").hide(); 
    $('input[type="radio"]').click(function(event) { 
     if($(this).val() == 'Hourly') { 
      $("#send_to_one").show(); 
      $("#send_to_two").hide(); 
     } else if($(this).val() == 'Daily'){ 
      $("#send_to_two").show(); 
      $("#send_to_one").hide(); 
     } else{ 
      $("#send_to_one").hide(); 
      $("#send_to_two").hide(); 
     } 
    }); 
}); 

答えて

0

両方selectドロップダウンがPOSTリクエストの一部として送信されているので、あなたがnilを取得しています。

これを解決する1つの方法は、コンテナを非表示にするときにその1つを無効にすることです。以下のようなもののためにJavaScriptを変更します。

$(function() { 
    var $one = $("#send_to_one"); 
    var $two = $("#send_to_two"); 
    var $three = $("#send_to_three"); 

    $one.hide().find("select").attr("disabled", true); 
    $two.hide().find("select").attr("disabled", true); 
    $three.hide().find("select").attr("disabled", true); 

    $('input[type="radio"]').click(function(event) { 
     if($(this).val() == 'Hourly') { 
      $one.show().find("select").attr('disabled', false); 
      $two.hide().find("select").attr('disabled', true); 
      $three.hide().find("select").attr("disabled", true); 
     } else if($(this).val() == 'Daily'){ 
      $two.show().find("select").attr('disabled', false); 
      $one.hide().find("select").attr('disabled', true); 
      $three.hide().find("select").attr("disabled", true); 
     } else if($(this).val() == 'Monthly'){ 
      $two.hide().find("select").attr('disabled', true); 
      $one.hide().find("select").attr('disabled', true); 
      $three.show().find("select").attr("disabled", false); 
     } else{ 
      $one.hide().find("select").attr("disabled", true); 
      $two.hide().find("select").attr("disabled", true); 
      $three.hide().find("select").attr("disabled", true); 
     } 
    }); 
}); 
+0

それはまだ作品はできません。

$(function() { var $one = $("#send_to_one"); var $two = $("#send_to_two"); $one.hide().find("select").attr("disabled", true); $two.hide().find("select").attr("disabled", true); $('input[type="radio"]').click(function(event) { if($(this).val() == 'Hourly') { $one.show().find("select").attr('disabled', false); $two.hide().find("select").attr('disabled', true); } else if($(this).val() == 'Daily'){ $two.show().find("select").attr('disabled', false); $one.hide().find("select").attr('disabled', true); } else{ $one.hide().find("select").attr("disabled", true); $two.hide().find("select").attr("disabled", true); } }); }); 

UPDATE 1

あなたのコメントによると、これを確認してください。 3つすべてのラジオボタンでコードを修正しました。最初のラジオボタンを選択すると、曜日が入力され、その日は空白にならないエラーが表示されますが、2番目のラジオボタンを選択した場合も同じことが言えます。しかし、私が3番目のラジオボタンを選択して日数を入力すると、それは成功です。 –

+0

私の更新を確認してください。しかし、最も重要なことは、paramsがサーバーに送信しているものを確認する必要があることです。あなたのケースでは、同じparamsまたは何も2回sendigがあります。それはあなたがJavaScriptを使用する方法に依存します – rogelio

+0

いいえ、私はそれを解決しました。ありがとう。これはドロップダウンリストなので、$ one.hide()。find( "select")。attr( "disabled"、true);書かれた。テキストフィールドの場合、このコード行をどのように修正すればよいですか? –

関連する問題