2017-02-27 58 views
0

這是我的view。有3個單選按鈕,用戶可以選擇其中一個。例如,當我選擇Hourly時,它將顯示#send_to_oneRails:提交下拉列表值提交無

它運行良好,顯示下拉列表,但是當我提交下拉列表的值變成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

你得到nil因爲兩個select下拉被作爲POST請求的一部分被髮送。

解決此問題的一種方法是在您隱藏其容器時禁用其中之一。更改JavaScript的類似:

$(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); 
     } 
    }); 
}); 

更新1

根據您的意見,看看這個:

$(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

仍然無法工作。我用三個單選按鈕修改了代碼。比方說,如果我選擇第一個單選按鈕,那麼我輸入一天中的數字,並顯示一天中不能爲空的錯誤,如果我選擇第二個單選按鈕,也是如此。但如果我選擇第三個單選按鈕並輸入一天的數量,那麼它是成功的。 –

+0

檢查我的更新。但最重要的是你需要檢查哪些參數發送到服務器。在你的情況發送兩次相同的params或什麼也沒有。這將取決於你如何使用JavaScript – rogelio

+0

好吧,我已經解決了它。謝謝。這是一個下拉列表,所以$ one.hide()。find(「select」)。attr(「disabled」,true);寫入。如果它是一個文本字段,那麼我應該如何修改這一行代碼呢? –