我有兩個選擇列表與複選框,我想使用相同的功能來管理它們,以避免代碼重複,但有選擇正確的複選框列表的問題,它不工作的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