使用 jQuery 禁用下拉选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11752097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:54:09  来源:igfitidea点击:

Disable Drop Down Option using jQuery

jqueryhtmlweb-deployment

提问by nasty

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

我需要在动态下拉菜单列表中禁用值为“- Sold Out -”的选项。如何使用 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>

回答by thecodeparadox

$("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);
});

回答by Tats_innit

Working demohttp://jsfiddle.net/BYkVW/orhttp://jsfiddle.net/BYkVW/1/

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

Hope it helps the needs :)

希望对需求有所帮助 :)

code

代码

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

or

或者

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

working image

工作形象

enter image description here

在此处输入图片说明

回答by Bbb

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

回答by t_m27

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

回答by gaurang171

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

在这里,我已经完成了上述查询的解决方案。演示链接如下:

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

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

HTML:

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

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

回答by ammad khan

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