简体   繁体   English

如何禁用下拉菜单中的选项

[英]How do you disable options in a drop down menu

This is a very short and simple question, is there a way in javascript to disable options in a drop down menu, for example: 这是一个非常简短的问题,javascript中是否有一种方法可以禁用下拉菜单中的选项,例如:

Do you want to use drop down menu? 您要使用下拉菜单吗?

If user selected yes then undisable options in drop down menu 如果用户选择是,则在下拉菜单中禁用选项

if user does not want to use dropbox then keep the options in the drop down menu disabled 如果用户不想使用保管箱,则禁用下拉菜单中的选项

Thanks 谢谢

CSS is purely for styling, if you want to disable a select box look into the readonly="readonly" property. CSS仅用于样式设置,如果要禁用选择框,请查看readonly="readonly"属性。 Keep in mind if you want to change the attribute like this on the fly I'd recommend using some javascript library like jQuery 请记住,如果您想即时更改属性,建议您使用jQuery之类的JavaScript库

You cannot disable any element using CSS, just stylize it. 您不能使用CSS禁用任何元素,只需对其进行样式化即可。 You should use JavaScript to do what you want. 您应该使用JavaScript来完成所需的工作。

In JS you may disable it using disabled property of a DOM object. 在JS中,您可以使用DOM对象的disabled属性来禁用它。

Here is a Jquery function that would handle what you're looking for 这是一个Jquery函数,可以处理您要查找的内容

    $(function () {
            $('#useDDL').click(function () {
                var satisfied = $('#useDDL:checked').val();
                if (satisfied != undefined) $('#DDL').removeAttr('disabled');
                else $('#DDL').attr('disabled', 'disabled');
            });
        });

Where DDL is the drop down menu and useDDL is a checkbox the user can check 其中DDL是下拉菜单,useDDL是用户可以检查的复选框

To "disable" an element with CSS you must set it to display: none or visibility: hidden . 要使用CSS“禁用”元素,您必须将其设置为display: nonevisibility: hidden

But if you want to work with events you will need JavaScript. 但是,如果要处理事件,则需要JavaScript。

You can better use jquery's show() and hide() functions to show or hide particular options based on what users select. 您可以更好地使用jquery的show()hide()函数根据用户选择显示或隐藏特定选项。 This should be the best approach. 这应该是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM