gpt4 book ai didi

javascript - 使用 jQuery 禁用 select 中的某些选项

转载 作者:行者123 更新时间:2023-11-28 19:42:38 27 4
gpt4 key购买 nike

我有以下 html 代码:

<select class="pointSelect" id="mySelect" tabindex="2" name="mySelect">
<option value="01" selected="selected">Choose Country</option>
<option>United States</option>
<option>Canada</option>
<option>United Kingdom</option>
<option>Czech Republic</option>
<option>Any other Country</option>
</select>

<select class="pointSelect" id="mySelect1" tabindex="3" name="mySelect1">
<option value="nothing" selected="selected">Choose Shipping Options</option>
<option>Free Shipping</option>
<option>Registered Airmail</option>
<option>Express Shipping</option>
</select>

和以下 jQuery 代码:

$("#mySelect").change(function() {
debugger;
var index = $(this).find("option:selected").index();
$("#mySelect1").find("option:lt("+index+")").attr("disabled","disabled");
$("#mySelect1").find("option:gt("+index+")").removeAttr("disabled");
});

Demo jsFiddle

如何仅针对“任何其他国家/地区”禁用免费送货。

最佳答案

在Jquery中使用Prop来禁用该选项。如果使用attr则无法再次启用和禁用选项

   $("#mySelect1").prop("disabled", true);
$("#mySelect").change(function () {
$("#mySelect1").prop("disabled", (this.value == '01'));
$("#mySelect1").find("option:eq(1)").prop("disabled", (this.value == 'Any other Country'));
});

DEMO

NEW UPDATED DEMO

关于javascript - 使用 jQuery 禁用 select 中的某些选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24794875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com