如何获得Multiselect的特定单击选项的值?

时间:2019-05-07 10:22:38

标签: javascript jquery

我有一个这样的下拉列表

<select id='companyList' class='js-select-Multiple' multiple = "true">
<option value='1'>ABC</option>
<option value='2'>PQR</option>
<option value='3'>XYZ</option>
</select>

当用户单击任何公司时,我想发出警报 例如如果用户单击XYZ,将弹出这样的警报“此公司不可用”

我尝试过:

    $(document).on('change', '#companyList', function (e)
    {
        alert(e.target.options[e.target.selectedIndex].text);
    });

但是它没有给出正确的值

<script>
        $('#companyList').multiselect({
            selectAllValue: 'multiselect-all',
            enableCaseInsensitiveFiltering: true,
            enableFiltering: true,
            maxHeight: '300',
            buttonWidth: '235',
            numberDisplayed: 2,
            nonSelectedText: 'Select One or More',
        });     
</script>  

1 个答案:

答案 0 :(得分:0)

您输入的身份证错误。您使用了companyList,但实际上ID是company

$('#companyList').multiselect({
            selectAllValue: 'multiselect-all',
            enableCaseInsensitiveFiltering: true,
            enableFiltering: true,
            maxHeight: '300',
            buttonWidth: '235',
            numberDisplayed: 2,
            nonSelectedText: 'Select One or More',
            onChange: function(element, checked) {
              console.log($(element).text());
            }
        });  
        
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>


<select id='companyList' class='js-select-Multiple' multiple = "true">
<option value='1'>ABC</option>
<option value='2'>PQR</option>
<option value='3'>XYZ</option>
</select>