2016-11-17 66 views
0

我有兩個選擇列表與複選框,我想使用相同的功能來管理它們,以避免代碼重複,但有選擇正確的複選框列表的問題,它不工作的ID:使用相同的功能展開復選框選擇列表

<div class="multiselect"> 
    <div class="selectBox" id="select1" onclick="showCheckboxes(this.id)"> 
     <select> 
      <option>Select deliverer</option> 
     </select> 
     <div class="overSelect"></div> 
    </div> 
    <div id="deliverer_checkboxes" style="height:100px;overflow:auto;"> 
     <% @deliverers.each do |deliverer| %> 
      <label for="<%= deliverer.id %>"><%= check_box_tag "deliverer_user_ids[]", deliverer.id %> <%= deliverer.name %></label> 
     <% end %> 
    </div> 
    </div> 

    <div class="multiselect"> 
    <div class="selectBox" id="select2" onclick="showCheckboxes(this.id)"> 
     <select> 
      <option>Select shopper</option> 
     </select> 
     <div class="overSelect"></div> 
    </div> 
    <div id="shopper_checkboxes" style="height:100px;overflow:auto;"> 
     <% @shoppers.each do |shopper| %> 
      <label for="<%= shopper.id %>"><%= check_box_tag "shopper_user_ids[]", shopper.id %> <%= shopper.name %></label> 
     <% end %> 
    </div> 
    </div> 
    <%= f.submit 'Search', class: 'btn btn-success' %> 
<% end %> 
</div> 

<script type="text/javascript"> 
var expanded = false; 
    function showCheckboxes(clicked_id) { 
     var checkboxes = document.getElementById(clicked_id); 
     if (!expanded) { 
      checkboxes.style.display = 'block'; 
      expanded = true; 
     } else { 
      checkboxes.style.display = 'none'; 
      expanded = false; 
     } 
    } 
</script> 

<style> 
    .multiselect { 
     width: 200px; 
     float:left; 
    } 
    .selectBox { 
     position: relative; 
    } 
    .selectBox select { 
     width: 100%; 
     font-weight: bold; 
    } 
    .overSelect { 
     position: absolute; 
     left: 0; right: 0; top: 0; bottom: 0; 
    } 
    #deliverer_checkboxes { 
     display: none; 
     border: 1px #dadada solid; 
    } 
    #deliverer_checkboxes label { 
     display: block; 
    } 
    #deliverer_checkboxes label:hover { 
     background-color: #1e90ff; 
    } 
    #shopper_checkboxes { 
     display: none; 
     border: 1px #dadada solid; 
    } 
    #shopper_checkboxes label { 
     display: block; 
    } 
    #shopper_checkboxes label:hover { 
     background-color: #1e90ff; 
    } 
</style> 
+0

這兩個問題你檢查它是否正確傳遞ID? (例如通過使用控制檯) – Niklas

+0

在我點擊兩次後,我的選擇列表消失 – user7067399

+0

這不是我要求的。在showCheckboxes函數中添加語句'console.log(clicked_id)'作爲第一條語句。然後加載頁面並按F12。看看它是否正確傳遞值。在這裏報告。 – Niklas

回答

0

而不是共享擴展變量,請直接從您單擊的元素檢查「擴展」標誌。一種方法是檢查顯示,因爲你正在切換它。

function showCheckboxes(clicked_id) { 
    var checkboxes = document.getElementById(clicked_id); 
    var display = checkboxes.style.display; 
    checkboxes.style.display = display !== 'none' ? 'none' : 'block'; 
} 
+0

當我點擊複選框列表兩次消失。與原始碼相同的情況。鉻。 – user7067399

+0

當我點擊它,一旦沒有任何反應。 – user7067399

+0

你想隱藏/顯示'deliverrer_checkboxes'和'shopper_checkboxes'嗎?如果是這種情況,您可以將'deliverrer_checkboxes'和'shopper_checkboxes'傳遞給每個'showCheckboxes'。 – phoa

0

我不知道你在找什麼做的,但你得到你所看到的行爲的原因是:

1)中的「點擊兩次」 - 您已設置的初始當方框展開時,expanded的值爲false,因此您需要實際點擊兩次以切換隱藏。您可以通過將其初始化設置爲true來糾正該問題

2)類似的問題存在於按照原樣重新使用代碼的情況。對於多個selectBox,您有一個expanded變量。所以無論你選擇哪一個selectBox將存儲在該變量中的值,該值將適用於所有其他selectBox

可以糾正的東西,如

function showCheckboxes(clicked_id) { 
     var checkboxes = document.getElementById(clicked_id); 

     var expanded = (checkboxes.style.display == 'none') ? false : true; 

     if (!expanded) { 
      checkboxes.style.display = 'block'; 
     } else { 
      checkboxes.style.display = 'none'; 
     } 
    } 

http://jsbin.com/lahulabilo/edit?html,js,output

+0

當點擊兩次時,它隱藏了整個選擇列表 – user7067399

+0

我已經在FF,Chrome和IE 11中再次測試了我的jsbin,並且每個選擇列表都隱藏了點擊一次。看着另一個答案,看來我們都不清楚你在找什麼東西。也許你可以用你的問題來澄清? –