如果在多选下拉列表中选中了多个选项,您如何隐藏/显示特定内容?
HTML
<select title="Choose option/s" class="form-control selectpicker" multiple>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<span id="legendWasSelected" class="text-hard-light margin-left-xxs">One option was selected</span>
<span id="legendWereSelected" class="text-hard-light margin-left-xxs" style="display:none">More than one option was selected</span>
答案 0 :(得分:2)
$('.selectpicker').change(function() {
if($(this).val().length > 1) {
$('legendWereSelected:nth-of-type(2)').show();
} else {
$('legendWereSelected:nth-of-type(2)').hide();
}
});
我还没有测试过,但这应该是正确的,请告诉我是否有错误
答案 1 :(得分:1)
使用:selected选择器,如下所示(使用jQuery):
$('#yourIDHere option:selected').length;
如果长度大于1,则选择了多个选项
编辑:(根据Deckerz评论)
您可以使用jQuery .val()
从多选中获取选定的值var selectedValues = $('#yourIDHere').val();