簡體   English   中英

檢查是否在下拉多項選擇中選擇了多個選項

[英]Checking if more than one option is selected in a Dropdown Multiple Select

如果在多選下拉菜單中選中了多個選項,如何隱藏/顯示特定內容?

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

我尚未測試過,但這應該是正確的,請告訴我是否有錯誤

使用:selected選擇器,如下所示(使用jQuery):

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

如果長度大於1,則您選擇了多個選項

編輯:(基於Deckerz的評論)

您可以使用jQuery .val()multiselect中獲取選定的值

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM