2017-08-17 82 views
0

如果在多选下拉列表中选中多个选项,如何隐藏/显示特定内容?检查是否在下拉多选中选择了多个选项

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> 

回答

2
$('.selectpicker').change(function() { 
    if($(this).val().length > 1) { 
     $('legendWereSelected:nth-of-type(2)').show(); 
    } else { 
     $('legendWereSelected:nth-of-type(2)').hide(); 
    } 
}); 

我还没有测试,但应该是基本正确选择从多选值,请告诉我,如果有一个错误

+0

谢谢,它的作品! – nicozica

+0

没问题,你能否将我的答案标记为正确,谢谢:) – Shard

1

使用:选择的选择,像这样(使用jQuery):

$('#yourIDHere option:selected').length; 

如果长度大于1,你有超过1个选项选择

编辑:(基于Deckerz评论)

您开始使用jQuery .val()

var selectedValues = $('#yourIDHere').val(); 
+0

增加的另一件事是,如果你想根据你可以使用的值做一些事情'var selectedValues = $('#yourIDHere')。val();' – Deckerz

+0

非常合理! – Winnie