答案 0 :(得分:0)
您可以使用map()
和filter()
获取所有选定的值。全部循环选择并隐藏所选选项。
要越过<li>
,您可以循环访问<li>
并检查是否使用includes()
选择了
$('select').change(function() {
var selected = $('select').map(function() {
return this.value;
}).get().filter(o => o !== '');
//Hide Selected on drop downs
if ($(this).val() != "") {
$("select").not(this).find("option").show().filter(function() {
return $(this).val() != "" && selected.includes($(this).val());
}).hide();
} else {
$("select").find("option").show().filter(function() {
return $(this).val() != "" && selected.includes($(this).val());
}).hide();
}
//Remove all class on li
$('ul li').removeClass('selected-option');
//Add class to all selected
$('ul li').each(function() {
if (selected.includes($(this).text())) $(this).addClass('selected-option');
})
});
.left {
float: left;
width: 75%;
}
.right {
float: right;
width: 25%;
list-style: none;
background: blue;
color: #fff;
}
.right li {
list-style: none;
}
.selected-option {
opacity: .5;
text-decoration: line-through;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="left">
<select name="one" class="mySelect">
<option value="">---</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="two" class="mySelect">
<option value="">---</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="three" class="mySelect">
<option value="">---</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="four" class="mySelect">
<option value="">---</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="five" class="mySelect">
<option value="">---</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
</div>
<div class="right">
<ul>
<li class="multiple-choice-list">One</li>
<li class="multiple-choice-list">Two</li>
<li class="multiple-choice-list">Three</li>
<li class="multiple-choice-list">Four</li>
<li class="multiple-choice-list">Five</li>
</ul>
</div>