如何在禁用选项jquery select2上禁用搜索?

时间:2017-03-10 05:58:45

标签: javascript jquery select2

这是我的榜样。已禁用my price选项。如果我使用select2搜索它仍然会出现结果。我想要的是,如何制作残疾选项并没有因此而出现?在这种情况下,如果我输入my price,我不希望my出现。有可能吗?



$(document).ready(function() {
 $(".selectvoc").select2();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<select class="selectvoc" name="voucher">
<optgroup label="voc1">
<option>vocvoc1</option>
<option disabled>my price1</option>
<option>vocvoc2</option>
<option disabled>my price2</option>	
<option>vocvoc3</option>
<option disabled>my price3</option>	
<optgroup label="voc2">
<option>vocvoc1</option>
<option disabled>my price1</option>
<option>vocvoc2</option>
<option disabled>my price2</option>	
<option>vocvoc3</option>
<option disabled>my price3</option>	
</select>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以在选项中包含已禁用的css样式,以便在您尝试搜索已禁用的某个时,它们将不会出现

    $(document).ready(function() {
     $(".selectvoc").select2();
    });
.select2-container--default .select2-results__option[aria-disabled=true] {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- begin snippet: js hide: false console: true babel: false -->

    <script src=""></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
    <select class="selectvoc" name="voucher">
    <optgroup label="voc1">
    <option>vocvoc1</option>
    <option disabled hidden>my price1</option>
    <option>vocvoc2</option>
    <option disabled>my price2</option>	
    <option>vocvoc3</option>
    <option disabled>my price3</option>	
    <optgroup label="voc2">
    <option>vocvoc1</option>
    <option disabled>my price1</option>
    <option>vocvoc2</option>
    <option disabled>my price2</option>	
    <option>vocvoc3</option>
    <option disabled>my price3</option>	
    </select>