Jquery 禁用下拉列表中的选项

发布于 2024-10-16 17:53:17 字数 374 浏览 2 评论 0原文

伙计们,我使用下面的代码来禁用使用 jQuery 的选项(jquery-1.4.2.min)。禁用发生在 Firefox 中,但不会发生在 IE 中。

<SELECT NAME="SCOPE" id="SCOPE">  
 <OPTION VALUE="G"> Global
 <OPTION VALUE="D"> Dynamic  
</SELECT>


 $("#SCOPE option[value='G']").attr("disabled","disabled");
 $("#SCOPE option[value='D']").attr("selected", "selected");

Guys i have used the following code to disable an option using jQuery (jquery-1.4.2.min).The disable happens in Firefox , but not in IE.

<SELECT NAME="SCOPE" id="SCOPE">  
 <OPTION VALUE="G"> Global
 <OPTION VALUE="D"> Dynamic  
</SELECT>


 $("#SCOPE option[value='G']").attr("disabled","disabled");
 $("#SCOPE option[value='D']").attr("selected", "selected");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-10-23 17:53:17

我想我可能是错的,但这可能是因为可以禁用选择而不是选项。由于 Firefox 很棒而 IE 很糟糕,你可以猜到为什么你会遇到这个问题。使用 css 使该选项的文本变灰。

然后在 jquery 上做这样的事情。

$('#SCOPE').change(function(){
  if($('#SCOPE option[value="'+$(this).val()+'"]').attr('disabled') == 'disabled'){
    alert('Its disabled you cannot select this option');
  }
});

顺便提一句。仔细检查代码,因为我还没有测试过这个:)

I think i might be wrong but it could be because the select rather than option can be disabled. Since firefox is great and IE sucks, well you can guess why :) you having that problem. Use css to grey out the text of that option.

then on jquery do something like this.

$('#SCOPE').change(function(){
  if($('#SCOPE option[value="'+$(this).val()+'"]').attr('disabled') == 'disabled'){
    alert('Its disabled you cannot select this option');
  }
});

BTW. double check the code as I have not tested this :)

兲鉂ぱ嘚淚 2024-10-23 17:53:17

我的看法与其他答案有点不同。

目的不是隐藏选项,而只是禁用它们(以保持 UI 一致)。

我的场景:

我在表单中有多个选择,当用户在其中一个选择中选择一个选项时,其他选择应该禁用此选项,反之亦然。用户被限制选择与已选择的选项相同的选项。我们通常禁用该选项,但 IE 7 不支持它。用户还可以选择添加新选择。

解决方案:

加载时:

如果浏览器是 IE7,则在填充选择并禁用其他选择上已选择的选项时,我向选项添加自定义属性(“data-ie7-disabled”)并更改颜色禁用选项为“#cccccc”(这是禁用选项的标准颜色)。这使得 UI 在各个浏览器中看起来都相同。

更改时:

我将上一个选项保存在局部变量中(这保存在焦点上)。

当用户尝试更改选项时,

用户选择一个全新的选项,该选项未在任何其他下拉列表中选择。然后,我循环遍历其他选择并更改颜色,并将自定义属性添加到其他选择上的此选定选项。

当用户选择一个已经选择的选项时(颜色变灰的选项)。我首先检查该选项是否具有此自定义属性。如果有则>我只是将该选项恢复到前一个选项,并显示一条错误消息“该选项已被选中或BLAH BLAH”。

当用户将其现有选项更改为未在任何其他下拉列表中选择的全新选项时。我再次循环遍历所有其他选择选项并删除其颜色以及自定义属性。

希望这有帮助。

My take is bit different to other answers.

The aim is not to hide the options but just make them disable(to keep the UI consistent).

My Scenario :

I have multiple selects in a form and when an user selects an option in one of the selects the other selects should disable this option and vice versa. User is restricted from selecting the same option which is already selected. We normally disable the option but for IE 7 which does not support it. User also gets an option to add new selects.

Solution :

On load :

If the browser is IE7 then while populating the the selects and disabling the already selected options on other selects I am adding a custom attribute to the option("data-ie7-disabled") and also changing the color of the disabled options to '#cccccc'(which is the standard color for disabled options). This makes the UI look same across browsers.

On Change :

I save the previous option in a local variable(this is saved on focus).

When a user tries to change an option

User selects a complete new option which is not selected in any other dropdown. Then I loop through other selects and change the color and add custom attribute to this selected option on other selects.

When user selects an option that is already selected(The option which has grayed out color). I check if the option has this custom attribute on it first. If it has then > I simply revert the option to the previous one with an error message saying "This option is already selected or BLAH BLAH".

When user changes his existing option to a brand new option which is not selected in any other dropdown's. I again loop through all the other select options and remove the color on it and also the custom attribute.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文