2016-11-17 60 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,并且每个选择列表都隐藏了点击一次。看着另一个答案,看来我们都不清楚你在找什么东西。也许你可以用你的问题来澄清? –