Disable Drop Down Option using jQuery

前端 未结 7 704
时光取名叫无心
时光取名叫无心 2021-01-04 07:35

I need to disable options with value \"- Sold Out -\" in a list of dynamic drop down menus. How can I do this easily with jQuery? Below is the HTML



        
相关标签:
7条回答
  • 2021-01-04 07:47
    function lockDownDropDownList(ddlName) {
        ddlName = "#" + ddlName;
        var chosenValue = $(ddlName).val();
    
        var downDownListItems = $(ddlName).children('option').map(function (i, e) {
            return e.value || e.innerText;
        }).get();
    
        downDownListItems.forEach(function (item) {
            if (item != chosenValue)
            {
                $("select option[value*='" + item + "']").prop('disabled', true);
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-04 07:52

    In case anyone wants to be able to disable a drop down list by text instead of value, here's what I did:

    $("#DDL option").filter(function () {
        return $(this).text() === "Text 1" ||
               $(this).text() === "Text 2" ||
               $(this).text() === "Text 3";
    }).prop("disabled", true);
    
    0 讨论(0)
  • 2021-01-04 07:53

    Here i have done solution for above query. demo link as below:

    Demo: http://codebins.com/bin/4ldqp92

    HTML:

     <select id="field_0_1" class="text_select" name="field_0_1" onChange="">
      <option value="">
        - Preferred Time -
      </option>
      <option value="- Sold Out -">
        - Sold Out -
      </option>
      <option value="2:30 - 4:00pm">
        2:30 - 4:00pm
      </option>
    </select>
    <select id="field_0_2" class="text_select" name="field_0_2" onChange="">
      <option value="">
        - Preferred Time -
      </option>
      <option value="- Sold Out -">
        - Sold Out -
      </option>
      <option value="2:30 - 4:00pm">
        2:30 - 4:00pm
      </option>
    </select>
    <select id="field_0_3" class="text_select" name="field_0_3" onChange="">
      <option value="">
        - Preferred Time -
      </option>
      <option value="- Sold Out -">
        - Sold Out -
      </option>
      <option value="2:30 - 4:00pm">
        2:30 - 4:00pm
      </option>
    </select>
    

    JQuery:

    $(function() {
        $("select").click(function() {
            $(this).find("option[value*='Sold Out']").prop("disabled", true);
        });
    });
    

    Demo: http://codebins.com/bin/4ldqp92

    0 讨论(0)
  • 2021-01-04 07:57

    Just use add class disabled to md-select-wrapper $("#selectFromMainId").addClass("disabled");

    md-select-wrapper disabled disabled

    0 讨论(0)
  • 2021-01-04 08:01
    $("#ddlList option[value='jquery']").attr("disabled","disabled");
    
    0 讨论(0)
  • 2021-01-04 08:03
    $("select option[value*='Sold Out']").prop('disabled',true);
            ​
    

    Demo

    According to edit

    $('#previous_select').on('change', function() {
       // after creating the option
       // try following
       $("select option[value*='Sold Out']").prop('disabled',true);
    });
    
    0 讨论(0)
提交回复
热议问题