禁用<select> <option>

时间:2017-08-15 19:38:14

标签: javascript jquery html

我有两个&lt; select&gt;我网页上的元素。看看这个小提琴。 如您所见,我们无法在原点和目的地中选择相同的选项。但是,只要选项被禁用,即使选择了任何其他元素,它也不会再次启用。 如何再次启用禁用选项?

2 个答案:

答案 0 :(得分:1)

$(document).ready(function(){

    $('#origin').change(function(){
        var airportOrigin=($(this).val());
        var airportDestination=$('#destination').val();
         $("#destination option").removeAttr("disabled");
         $("#destination option:contains('"+ airportOrigin + "')").attr("disabled",true);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label for="origin">Origin:<p> </p></label>
     
<select id= "origin"  data-live-search="true" class="selectpicker form-control" data-size="5" title="Select Destination">
<option data-subtext="DEL" data-tokens="DEL New Delhi">New Delhi</option>
<option data-subtext="BOM" data-tokens="BOM Mumbai">Mumbai</option>
<option value="Kolkata" data-subtext="CCU" data-tokens="CCU Kolkata">Kolkata</option>
<option data-subtext="BLR" data-tokens="BLR Bangalore">Bangalore</option>
<option data-subtext="MAA" data-tokens="MAA Chennai">Chennai</option>
<option data-subtext="PNQ" data-tokens="PNQ Pune">Pune</option>
<option data-subtext="GOI" data-tokens="GOI Goa">Goa</option>
<option data-subtext="GAU" data-tokens="GAU Guwahati">Guwahati</option>
<option data-subtext="ISK" data-tokens="ISK Gandhinagar">Gandhinagar</option>
<option data-subtext="IXJ" data-tokens="IXJ Jammu">Jammu</option>
<option data-subtext="BHO" data-tokens="BHI Bhopal">Bhopal</option>
<option data-subtext="IXA" data-tokens="IXA Agartala">Agartala</option>
</select>
</div>
              
<div class="form-group">
<label for="destination">Destination:<p> </p></label>
<select id= "destination"  data-live-search="true" class="selectpicker form-control" data-size="5" title="Select Destination">
<option data-subtext="DEL" data-tokens="DEL New Delhi">New Delhi</option>
<option data-subtext="BOM" data-tokens="BOM Mumbai">Mumbai</option>
<option value="Kolkata" data-subtext="CCU" data-tokens="CCU Kolkata">Kolkata</option>
<option data-subtext="BLR" data-tokens="BLR Bangalore">Bangalore</option>
<option data-subtext="MAA" data-tokens="MAA Chennai">Chennai</option>
<option data-subtext="PNQ" data-tokens="PNQ Pune">Pune</option>
<option data-subtext="GOI" data-tokens="GOI Goa">Goa</option>
<option data-subtext="GAU" data-tokens="GAU Guwahati">Guwahati</option>
<option data-subtext="ISK" data-tokens="ISK Gandhinagar">Gandhinagar</option>
<option data-subtext="IXJ" data-tokens="IXJ Jammu">Jammu</option>
<option data-subtext="BHO" data-tokens="BHI Bhopal">Bhopal</option>
<option data-subtext="IXA" data-tokens="IXA Agartala">Agartala</option>
</select>
</div>

答案 1 :(得分:1)

试试这个。

$(document).ready(function(){

    $('#origin').change(function(){

        var airportOrigin=($(this).val());
        var airportDestination=$('#destination').val();

         $("#destination option").attr("disabled", false);

         $("#destination option:contains('"+ airportOrigin + "')").attr("disabled",true);

    });

});