我有两个选择列表与复选框,我想使用相同的功能来管理它们,以避免代码重复,但有选择正确的复选框列表的问题,它不工作的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>
这两个问题你检查它是否正确传递ID? (例如通过使用控制台) – Niklas
在我点击两次后,我的选择列表消失 – user7067399
这不是我要求的。在showCheckboxes函数中添加语句'console.log(clicked_id)'作为第一条语句。然后加载页面并按F12。看看它是否正确传递值。在这里报告。 – Niklas