使用jQuery禁用下拉选项

时间:2012-08-01 03:14:06

标签: jquery html web-deployment

我需要在动态下拉菜单列表中禁用值为“ - 售完 - ”的选项。如何使用jQuery轻松完成此操作?下面是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>

7 个答案:

答案 0 :(得分:37)

$("select option[value*='Sold Out']").prop('disabled',true);
        ​

<强> Demo

根据编辑

$('#previous_select').on('change', function() {
   // after creating the option
   // try following
   $("select option[value*='Sold Out']").prop('disabled',true);
});

答案 1 :(得分:5)

工作演示 http://jsfiddle.net/BYkVW/ http://jsfiddle.net/BYkVW/1/

希望它有助于满足需求:)

<强>码

$("#field_0_1 option[value='- Sold Out -']").attr('disabled','disabled');
        ​

$("#field_0_1 option[value='- Sold Out -']").prop('disabled','disabled');

工作形象

enter image description here

答案 2 :(得分:1)

这里我已经为上面的查询做了解决方案。演示链接如下:

演示: 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);
    });
});

演示: http://codebins.com/bin/4ldqp92

答案 3 :(得分:1)

$("#ddlList option[value='jquery']").attr("disabled","disabled");

答案 4 :(得分:1)

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);
        }
    });
}

答案 5 :(得分:1)

万一有人希望能够通过文本而不是值来禁用下拉列表,这就是我所做的:

$("#DDL option").filter(function () {
    return $(this).text() === "Text 1" ||
           $(this).text() === "Text 2" ||
           $(this).text() === "Text 3";
}).prop("disabled", true);

答案 6 :(得分:0)

只需将添加类禁用到 md-select-wrapper $(“#selectFromMainId”)。addClass(“ disabled”);

已禁用md-select-wrapper