javascript 如何使下拉列表不可选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10378804/
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
How to make dropdown list unselectable?
提问by AKZap
How can I make the <h:selectOneMenu>
dropdown list unselectable?
如何使<h:selectOneMenu>
下拉列表不可选择?
It is not like using disabled
. I just want to make the dropdown list options unselectable and to appear with a different (inactive) style class.
它不像使用disabled
. 我只想使下拉列表选项不可选择并以不同的(非活动)样式类显示。
How can I achieve this? By a JSF component attribute or CSS or JavaScript?
我怎样才能做到这一点?通过 JSF 组件属性或 CSS 或 JavaScript?
回答by SanamShaikh
<asp:DropDownList ID="ddlName" runat="server" style="pointer-events: none; cursor: default;">
</asp:DropDownList>
Add style
attribute to DropDownList
as style="pointer-events: none; cursor: default;"
. This will work succesfully, I have tried this.
将style
属性添加到DropDownList
as style="pointer-events: none; cursor: default;"
。这会成功,我已经试过了。
回答by Ryan
If I understand you correctly, all you need to do is use the disabled
attribute.
如果我理解正确,您需要做的就是使用该disabled
属性。
So, if you have a select tag like this:
所以,如果你有一个这样的选择标签:
<select>
<option>Value</option>
<option>Value</option>
<option>Value</option>
</select>
Simply change it to this:
只需将其更改为:
<select disabled>
<option>Value</option>
<option>Value</option>
<option>Value</option>
</select>
Some doctypes require all attributes to have values, in order to be valid. In that case, you can just use disabled="true"
instead.
一些文档类型要求所有属性都具有值,以便有效。在这种情况下,您可以disabled="true"
改为使用。
回答by Matt Handy
According to this answer, some browsers allow to change the style of disabled components (non-IE), some not (IE). However, the answer is 3 years old - a long time for browsers. So you could try a solution like:
根据这个答案,有些浏览器允许更改禁用组件的样式(非 IE),有些则不允许(IE)。然而,答案是 3 岁——对于浏览器来说很长一段时间。因此,您可以尝试以下解决方案:
<h:selectOneMenu disabled="true" style="..." ...>
... and check the result with different browsers.
...并使用不同的浏览器检查结果。
Or alternatively use the readonly
attribute:
或者使用readonly
属性:
<h:selectOneMenu readonly="true" style="..." ...>