使用JQuery动态禁用多选选项

时间:2017-03-18 05:07:17

标签: javascript jquery multi-select

我有两个多选择下拉列表,其中包含相同的值集。

如果您在第一个下拉列表中选择一个选项,则应在第二个下拉列表中禁用该选项。

我能够为第一个选项而不是后续选项。

请帮助

<h4>Subject to </h4> <br>
                    <div class="form-group">
                        <label for="carrier">Charges * </label>
                        <select placeholder="Choose " class="form-control chosen-select" tabindex="2" name="subject_to[]" id="subject_to_id" width="40%"multiple>
                        <!-- chosen-select -->
                            <option value=""></option>  
                            <option value="CURRENCY" id="sub_opt1">CURRENCY</option>
                            <option value="BUNKER" id="sub_opt2">BUNKER</option>
                            <option value="LOW SULFUR" id="sub_opt3">LOW SULFUR</option>
                            <option value="CANAL" id="sub_opt4">CANAL</option>
                            <option value="GULF" id="sub_opt5">GULF</option>
                            <option value="PIRACY" id="sub_opt6">PIRACY</option>
                            <option value="WAR RISK" id="sub_opt7">WAR RISK</option>
                            <option value="CONGESTION" id="sub_opt8">CONGESTION</option>
                            <option value="PEAK SEASON" id="sub_opt9">PEAK SEASON</option>
                            <option value="RATE INCREASE" id="sub_opt10">RATE INCREASE</option>
                            <option value="EMERGENCY" id="sub_opt11">EMERGENCY</option>
                            <option value="WINTER" id="sub_opt1">WINTER</option>
                            <option value="DRAFT LIMITATION" id="sub_opt12">DRAFT LIMITATION</option>
                            <option value="OTHER" id="sub_opt13">OTHER</option>
                            <option value="TERMINAL HANDLING" id="sub_opt14">TERMINAL HANDLING</option>
                            <option value="BOOKING" id="sub_opt15">BOOKING</option>
                            <option value="DOCUMENTATION" id="sub_opt16">DOCUMENTATION</option>
                            <option value="TELEX" id="sub_opt17">TELEX</option>
                            <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="sub_opt18">EXPORT DECLARATION (ENS, AMS, etc.)</option>
                            <option value="PORT CHARGE" id="sub_opt19">PORT CHARGE</option>
                            <option value="SECURITY" id="sub_opt20">SECURITY</option>
                            <option value="SEAL" id="sub_opt21">SEAL</option>
                            <option value="CONTAINER INSPECTION" id="sub_opt22">CONTAINER INSPECTION</option>
                            <option value="CONTAINER WEIGHTING" id="sub_opt23">CONTAINER WEIGHTING</option>
                            <option value="CIC/EBS" id="sub_opt24">CIC/EBS</option>
                        </select>
                    </div>

                </div>
                <div class="col-md-4">
                <h4> Inclusive </h4> <br>
                    <div class="form-group">
                        <label for="inclusive"> Charges * </label>
                        <select placeholder="" class="form-control" tabindex="2" id="inclusive_id" name="inclusive[]" multiple>
                            <!-- chosen-select -->
                            <option value=""></option>  
                            <option value="CURRENCY" id="inc_opt1">CURRENCY</option>
                            <option value="BUNKER" id="inc_opt2">BUNKER</option>
                            <option value="LOW SULFUR" id="inc_opt3">LOW SULFUR</option>
                            <option value="CANAL" id="inc_opt4">CANAL</option>
                            <option value="GULF" id="inc_opt5">GULF</option>
                            <option value="PIRACY" id="inc_opt6">PIRACY</option>
                            <option value="WAR RISK" id="inc_opt7">WAR RISK</option>
                            <option value="CONGESTION" id="inc_opt8">CONGESTION</option>
                            <option value="PEAK SEASON" id="inc_opt9">PEAK SEASON</option>
                            <option value="RATE INCREASE" id="inc_opt10">RATE INCREASE</option>
                            <option value="EMERGENCY" id="inc_opt11">EMERGENCY</option>
                            <option value="WINTER" id="inc_opt1">WINTER</option>
                            <option value="DRAFT LIMITATION" id="inc_opt12">DRAFT LIMITATION</option>
                            <option value="OTHER" id="inc_opt13">OTHER</option>
                            <option value="TERMINAL HANDLING" id="inc_opt14">TERMINAL HANDLING</option>
                            <option value="BOOKING" id="inc_opt15">BOOKING</option>
                            <option value="DOCUMENTATION" id="inc_opt16">DOCUMENTATION</option>
                            <option value="TELEX" id="inc_opt17">TELEX</option>
                            <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="inc_opt18">EXPORT DECLARATION (ENS, AMS, etc.)</option>
                            <option value="PORT CHARGE" id="inc_opt19">PORT CHARGE</option>
                            <option value="SECURITY" id="inc_opt20">SECURITY</option>
                            <option value="SEAL" id="inc_opt21">SEAL</option>
                            <option value="CONTAINER INSPECTION" id="inc_opt22">CONTAINER INSPECTION</option>
                            <option value="CONTAINER WEIGHTING" id="inc_opt23">CONTAINER WEIGHTING</option>
                            <option value="CIC/EBS" id="inc_opt24">CIC/EBS</option>
                        </select>
                    </div>



    $('#subject_to_id').on('change', function (e) {
        var optionSelected = $("option:selected", this);
        var idSelected = this.value;

        var idSelected = $(this).children(":selected").attr("id");
        var str = idSelected;

        //alert(str + " "+str.length)

        var inc_id = "#inc_"+str.substring(4, str.length);

        //alert(inc_id);
        $(inc_id).attr("disabled","disabled");

});

3 个答案:

答案 0 :(得分:1)

这可以通过相对选择器来完成。我为此使用option点击事件。然后我抓住另一个表单组并修改它。它适用于两种方式,这意味着如果您从选择A中选择,则选择B将被修改,反之亦然。

&#13;
&#13;
$("option").click(function () {
    $otherFormGroup = $(".form-group").not(
         $(this)
            .parents(".form-group:first")
    ); //grabs the other form group
    $otherFormGroup.find("option").attr("disabled", false); //resets all other banned items
    $otherFormGroup.find("option[value='"+$(this).attr("value")+"']")
        .attr("disabled",true); //disabled selected item in the other select area
});
&#13;
<!-- Modified HTML to only demostrate content better in snippet, you can leave your HTML as is. -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Subject To:
<br><br>
<div class="form-group  form-group0">
   <label for="carrier">Charges * </label>
   <select placeholder="Choose " class="form-control chosen-select" tabindex="2" name="subject_to[]" id="subject_to_id" width="40%"multiple>
      <!-- chosen-select -->
      <option value=""></option>
      <option value="CURRENCY" id="sub_opt1">CURRENCY</option>
      <option value="BUNKER" id="sub_opt2">BUNKER</option>
      <option value="LOW SULFUR" id="sub_opt3">LOW SULFUR</option>
      <option value="CANAL" id="sub_opt4">CANAL</option>
      <option value="GULF" id="sub_opt5">GULF</option>
      <option value="PIRACY" id="sub_opt6">PIRACY</option>
      <option value="WAR RISK" id="sub_opt7">WAR RISK</option>
      <option value="CONGESTION" id="sub_opt8">CONGESTION</option>
      <option value="PEAK SEASON" id="sub_opt9">PEAK SEASON</option>
      <option value="RATE INCREASE" id="sub_opt10">RATE INCREASE</option>
      <option value="EMERGENCY" id="sub_opt11">EMERGENCY</option>
      <option value="WINTER" id="sub_opt1">WINTER</option>
      <option value="DRAFT LIMITATION" id="sub_opt12">DRAFT LIMITATION</option>
      <option value="OTHER" id="sub_opt13">OTHER</option>
      <option value="TERMINAL HANDLING" id="sub_opt14">TERMINAL HANDLING</option>
      <option value="BOOKING" id="sub_opt15">BOOKING</option>
      <option value="DOCUMENTATION" id="sub_opt16">DOCUMENTATION</option>
      <option value="TELEX" id="sub_opt17">TELEX</option>
      <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="sub_opt18">EXPORT DECLARATION (ENS, AMS, etc.)</option>
      <option value="PORT CHARGE" id="sub_opt19">PORT CHARGE</option>
      <option value="SECURITY" id="sub_opt20">SECURITY</option>
      <option value="SEAL" id="sub_opt21">SEAL</option>
      <option value="CONTAINER INSPECTION" id="sub_opt22">CONTAINER INSPECTION</option>
      <option value="CONTAINER WEIGHTING" id="sub_opt23">CONTAINER WEIGHTING</option>
      <option value="CIC/EBS" id="sub_opt24">CIC/EBS</option>
   </select>
</div>

From:
<br><br>
<div class="form-group form-group1">
   <label for="carrier">Charges * </label>
   <select placeholder="Choose " class="form-control chosen-select" tabindex="2" name="subject_to[]" id="subject_to_id" width="40%"multiple>
      <!-- chosen-select -->
      <option value=""></option>
      <option value="CURRENCY" id="sub_opt1">CURRENCY</option>
      <option value="BUNKER" id="sub_opt2">BUNKER</option>
      <option value="LOW SULFUR" id="sub_opt3">LOW SULFUR</option>
      <option value="CANAL" id="sub_opt4">CANAL</option>
      <option value="GULF" id="sub_opt5">GULF</option>
      <option value="PIRACY" id="sub_opt6">PIRACY</option>
      <option value="WAR RISK" id="sub_opt7">WAR RISK</option>
      <option value="CONGESTION" id="sub_opt8">CONGESTION</option>
      <option value="PEAK SEASON" id="sub_opt9">PEAK SEASON</option>
      <option value="RATE INCREASE" id="sub_opt10">RATE INCREASE</option>
      <option value="EMERGENCY" id="sub_opt11">EMERGENCY</option>
      <option value="WINTER" id="sub_opt1">WINTER</option>
      <option value="DRAFT LIMITATION" id="sub_opt12">DRAFT LIMITATION</option>
      <option value="OTHER" id="sub_opt13">OTHER</option>
      <option value="TERMINAL HANDLING" id="sub_opt14">TERMINAL HANDLING</option>
      <option value="BOOKING" id="sub_opt15">BOOKING</option>
      <option value="DOCUMENTATION" id="sub_opt16">DOCUMENTATION</option>
      <option value="TELEX" id="sub_opt17">TELEX</option>
      <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="sub_opt18">EXPORT DECLARATION (ENS, AMS, etc.)</option>
      <option value="PORT CHARGE" id="sub_opt19">PORT CHARGE</option>
      <option value="SECURITY" id="sub_opt20">SECURITY</option>
      <option value="SEAL" id="sub_opt21">SEAL</option>
      <option value="CONTAINER INSPECTION" id="sub_opt22">CONTAINER INSPECTION</option>
      <option value="CONTAINER WEIGHTING" id="sub_opt23">CONTAINER WEIGHTING</option>
      <option value="CIC/EBS" id="sub_opt24">CIC/EBS</option>
   </select>
</div>
&#13;
&#13;
&#13;

只是抬头,hardik prajapati后的帖子在您尝试几次后没有正确重置选项。

答案 1 :(得分:1)

以前的答案不适合多项选择,我修改了代码以匹配多项选择

&#13;
&#13;
$(".form-group:first option").click(function () {
    var selecteds = $('option:selected', $(this).parent());
    $secondFormGroup = $(".form-group:eq(1)");
    $secondFormGroup.find("option").attr("disabled", false);
    selecteds.each(function (i, el) {
    		var id = this.id.substr(7);
        $('#inc_opt' + id).attr('disabled', true);
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Subject to </h4> <br>
                    <div class="form-group">
                        <label for="carrier">Charges * </label>
                        <select placeholder="Choose " class="form-control chosen-select" tabindex="2" name="subject_to[]" id="subject_to_id" width="40%"multiple>
                        <!-- chosen-select -->
                            <option value=""></option>  
                            <option value="CURRENCY" id="sub_opt1">CURRENCY</option>
                            <option value="BUNKER" id="sub_opt2">BUNKER</option>
                            <option value="LOW SULFUR" id="sub_opt3">LOW SULFUR</option>
                            <option value="CANAL" id="sub_opt4">CANAL</option>
                            <option value="GULF" id="sub_opt5">GULF</option>
                            <option value="PIRACY" id="sub_opt6">PIRACY</option>
                            <option value="WAR RISK" id="sub_opt7">WAR RISK</option>
                            <option value="CONGESTION" id="sub_opt8">CONGESTION</option>
                            <option value="PEAK SEASON" id="sub_opt9">PEAK SEASON</option>
                            <option value="RATE INCREASE" id="sub_opt10">RATE INCREASE</option>
                            <option value="EMERGENCY" id="sub_opt11">EMERGENCY</option>
                            <option value="WINTER" id="sub_opt1">WINTER</option>
                            <option value="DRAFT LIMITATION" id="sub_opt12">DRAFT LIMITATION</option>
                            <option value="OTHER" id="sub_opt13">OTHER</option>
                            <option value="TERMINAL HANDLING" id="sub_opt14">TERMINAL HANDLING</option>
                            <option value="BOOKING" id="sub_opt15">BOOKING</option>
                            <option value="DOCUMENTATION" id="sub_opt16">DOCUMENTATION</option>
                            <option value="TELEX" id="sub_opt17">TELEX</option>
                            <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="sub_opt18">EXPORT DECLARATION (ENS, AMS, etc.)</option>
                            <option value="PORT CHARGE" id="sub_opt19">PORT CHARGE</option>
                            <option value="SECURITY" id="sub_opt20">SECURITY</option>
                            <option value="SEAL" id="sub_opt21">SEAL</option>
                            <option value="CONTAINER INSPECTION" id="sub_opt22">CONTAINER INSPECTION</option>
                            <option value="CONTAINER WEIGHTING" id="sub_opt23">CONTAINER WEIGHTING</option>
                            <option value="CIC/EBS" id="sub_opt24">CIC/EBS</option>
                        </select>
                    </div>

                </div>
                <div class="col-md-4">
                <h4> Inclusive </h4> <br>
                    <div class="form-group">
                        <label for="inclusive"> Charges * </label>
                        <select placeholder="" class="form-control" tabindex="2" id="inclusive_id" name="inclusive[]" multiple>
                            <!-- chosen-select -->
                            <option value=""></option>  
                            <option value="CURRENCY" id="inc_opt1">CURRENCY</option>
                            <option value="BUNKER" id="inc_opt2">BUNKER</option>
                            <option value="LOW SULFUR" id="inc_opt3">LOW SULFUR</option>
                            <option value="CANAL" id="inc_opt4">CANAL</option>
                            <option value="GULF" id="inc_opt5">GULF</option>
                            <option value="PIRACY" id="inc_opt6">PIRACY</option>
                            <option value="WAR RISK" id="inc_opt7">WAR RISK</option>
                            <option value="CONGESTION" id="inc_opt8">CONGESTION</option>
                            <option value="PEAK SEASON" id="inc_opt9">PEAK SEASON</option>
                            <option value="RATE INCREASE" id="inc_opt10">RATE INCREASE</option>
                            <option value="EMERGENCY" id="inc_opt11">EMERGENCY</option>
                            <option value="WINTER" id="inc_opt1">WINTER</option>
                            <option value="DRAFT LIMITATION" id="inc_opt12">DRAFT LIMITATION</option>
                            <option value="OTHER" id="inc_opt13">OTHER</option>
                            <option value="TERMINAL HANDLING" id="inc_opt14">TERMINAL HANDLING</option>
                            <option value="BOOKING" id="inc_opt15">BOOKING</option>
                            <option value="DOCUMENTATION" id="inc_opt16">DOCUMENTATION</option>
                            <option value="TELEX" id="inc_opt17">TELEX</option>
                            <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="inc_opt18">EXPORT DECLARATION (ENS, AMS, etc.)</option>
                            <option value="PORT CHARGE" id="inc_opt19">PORT CHARGE</option>
                            <option value="SECURITY" id="inc_opt20">SECURITY</option>
                            <option value="SEAL" id="inc_opt21">SEAL</option>
                            <option value="CONTAINER INSPECTION" id="inc_opt22">CONTAINER INSPECTION</option>
                            <option value="CONTAINER WEIGHTING" id="inc_opt23">CONTAINER WEIGHTING</option>
                            <option value="CIC/EBS" id="inc_opt24">CIC/EBS</option>
                        </select>
                    </div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

在两个选择中,Id值在CURRENCY和WINTER中相同,我已经更改了它,并且在脚本中将inc_id对象放入函数中。

$('#subject_to_id').on('change', function (e) {
      var optionSelected = $("option:selected", this);
      var idSelected = this.value;

      var idSelected = $(this).children(":selected").attr("id");
      var str = idSelected;

      //alert(str + " "+str.length)

      var inc_id = "#inc_" + str.substring(4, str.length);

      //alert(inc_id);
      $(inc_id).attr("disabled", "disabled");

   });
<script src="https://code.jquery.com/jquery-1.11.3.js"></script> 
<div class="col-md-4">
   <h4>Subject to </h4> <br>
   <div class="form-group">
      <label for="carrier">Charges * </label>
      <select class="form-control chosen-select" tabindex="2" name="subject_to[]" id="subject_to_id" multiple >
         <!-- chosen-select -->
         <option value=""></option>  
         <option value="CURRENCY" id="sub_opt1">CURRENCY</option>
         <option value="BUNKER" id="sub_opt2">BUNKER</option>
         <option value="LOW SULFUR" id="sub_opt3">LOW SULFUR</option>
         <option value="CANAL" id="sub_opt4">CANAL</option>
         <option value="GULF" id="sub_opt5">GULF</option>
         <option value="PIRACY" id="sub_opt6">PIRACY</option>
         <option value="WAR RISK" id="sub_opt7">WAR RISK</option>
         <option value="CONGESTION" id="sub_opt8">CONGESTION</option>
         <option value="PEAK SEASON" id="sub_opt9">PEAK SEASON</option>
         <option value="RATE INCREASE" id="sub_opt10">RATE INCREASE</option>
         <option value="EMERGENCY" id="sub_opt11">EMERGENCY</option>
         <option value="WINTER" id="sub_opt12">WINTER</option>
         <option value="DRAFT LIMITATION" id="sub_opt13">DRAFT LIMITATION</option>
         <option value="OTHER" id="sub_opt14">OTHER</option>
         <option value="TERMINAL HANDLING" id="sub_opt15">TERMINAL HANDLING</option>
         <option value="BOOKING" id="sub_opt16">BOOKING</option>
         <option value="DOCUMENTATION" id="sub_opt17">DOCUMENTATION</option>
         <option value="TELEX" id="sub_opt18">TELEX</option>
         <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="sub_opt19">EXPORT DECLARATION (ENS, AMS, etc.)</option>
         <option value="PORT CHARGE" id="sub_opt20">PORT CHARGE</option>
         <option value="SECURITY" id="sub_opt21">SECURITY</option>
         <option value="SEAL" id="sub_opt22">SEAL</option>
         <option value="CONTAINER INSPECTION" id="sub_opt23">CONTAINER INSPECTION</option>
         <option value="CONTAINER WEIGHTING" id="sub_opt24">CONTAINER WEIGHTING</option>
         <option value="CIC/EBS" id="sub_opt25">CIC/EBS</option>
      </select>
   </div>
</div>
<div class="col-md-4">        

   <h4> Inclusive </h4> <br>
   <div class="form-group">
      <label for="inclusive"> Charges * </label>
      <select class="form-control" tabindex="2" id="inclusive_id" name="inclusive[]" multiple>
         <!-- chosen-select -->
         <option value=""></option>  
         <option value="CURRENCY" id="inc_opt1">CURRENCY</option>
         <option value="BUNKER" id="inc_opt2">BUNKER</option>
         <option value="LOW SULFUR" id="inc_opt3">LOW SULFUR</option>
         <option value="CANAL" id="inc_opt4">CANAL</option>
         <option value="GULF" id="inc_opt5">GULF</option>
         <option value="PIRACY" id="inc_opt6">PIRACY</option>
         <option value="WAR RISK" id="inc_opt7">WAR RISK</option>
         <option value="CONGESTION" id="inc_opt8">CONGESTION</option>
         <option value="PEAK SEASON" id="inc_opt9">PEAK SEASON</option>
         <option value="RATE INCREASE" id="inc_opt10">RATE INCREASE</option>
         <option value="EMERGENCY" id="inc_opt11">EMERGENCY</option>
         <option value="WINTER" id="inc_opt12">WINTER</option>
         <option value="DRAFT LIMITATION" id="inc_opt13">DRAFT LIMITATION</option>
         <option value="OTHER" id="inc_opt14">OTHER</option>
         <option value="TERMINAL HANDLING" id="inc_opt15">TERMINAL HANDLING</option>
         <option value="BOOKING" id="inc_opt16">BOOKING</option>
         <option value="DOCUMENTATION" id="inc_opt17">DOCUMENTATION</option>
         <option value="TELEX" id="inc_opt18">TELEX</option>
         <option value="EXPORT DECLARATION (ENS, AMS, etc.)" id="inc_opt19">EXPORT DECLARATION (ENS, AMS, etc.)</option>
         <option value="PORT CHARGE" id="inc_opt20">PORT CHARGE</option>
         <option value="SECURITY" id="inc_opt21">SECURITY</option>
         <option value="SEAL" id="inc_opt22">SEAL</option>
         <option value="CONTAINER INSPECTION" id="inc_opt23">CONTAINER INSPECTION</option>
         <option value="CONTAINER WEIGHTING" id="inc_opt24">CONTAINER WEIGHTING</option>
         <option value="CIC/EBS" id="inc_opt25">CIC/EBS</option>
      </select>
   </div>
</div>