默认情况下如何显示禁用 HTML 选择选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22033922/
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 show disable HTML select option in by default?
提问by Ankit Sharma
I am new to HTML and PHP and want to achieve a drop-down menu from the mysql table and hard-coded too. I have multiple select in my page, One of them is
我是 HTML 和 PHP 的新手,希望从 mysql 表中实现下拉菜单,并且也进行硬编码。我的页面中有多个选择,其中之一是
<select name="tagging">
<option value="">Choose Tagging</option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
Problem is now that user can also select "Choose Tagging" as his tagging but i only want to provide him to chose from available three. I used disable as
现在的问题是用户也可以选择“选择标签”作为他的标签,但我只想让他从可用的三个中进行选择。我使用禁用作为
<select name="tagging">
<option value="" disabled="disabled">Choose Tagging</option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
But now "Option A" became the default one. So i want to set "Choose Tagging" as by default and also want to disable it from selection. Is it a way to do this. Same thing need to be done with other select which will fetch data from Mysql. Any suggestion will be appreciable.
但现在“选项A”成为默认选项。所以我想将“选择标记”设置为默认值,并且还想从选择中禁用它。有没有办法做到这一点。其他 select 也需要做同样的事情,这些 select 将从 Mysql 中获取数据。任何建议将是可观的。
回答by Cris
use
用
<option selected="true" disabled="disabled">Choose Tagging</option>
回答by Pradeep Kumar Prabaharan
In HTML5
, to select a disabled option:
在HTML5
, 中选择禁用的选项:
<option selected disabled>Choose Tagging</option>
回答by Mandrake
I know you ask how to disable the option, but I figure the end users visual outcome is the same with this solution, although it is probably marginally less resource demanding.
我知道您问如何禁用该选项,但我认为最终用户的视觉结果与此解决方案相同,尽管它的资源需求可能略低。
Use the optgrouptag, like so :
使用optgroup标签,如下所示:
<select name="tagging">
<optgroup label="Choose Tagging">
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</optgroup>
</select>
回答by Benjamin
Electron + React Let your two first options be like this
Electron + React 让你的两个第一个选择是这样的
<option hidden="true>Choose Tagging</option>
<option disabled="disabled" default="true">Choose Tagging</option>
First to display when closed Second to display first when the list opens
关闭时第一个显示 第二个列表打开时首先显示
回答by Thielicious
Use hidden
.
使用hidden
.
<select>
<option hidden>Choose</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
This doesn't unset it but you can however hide it in the options while it's displayed by default.
这不会取消设置,但您可以在默认情况下将其隐藏在选项中。
回答by c0degeas
Another SELECT tag solution for those who want to keep first option blank.
对于那些希望将第一个选项保留为空白的人来说,另一个 SELECT 标签解决方案。
<label>Unreal :</label>
<select name="unreal">
<option style="display:none"></option>
<option>Money</option>
<option>Country</option>
<option>God</option>
</select>
回答by Siddharth Shukla
we can disable using this technique.
我们可以禁用使用这种技术。
<select class="form-control" name="option_select">
<option selected="true" disabled="disabled">Select option </option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
回答by Ronak Bokaria
selected disabled="true"
Use this. It will work in new browsers
用这个。它将在新的浏览器中工作
回答by Cybernetic
If you are using jQuery to fill your select element, you can use this:
如果你使用 jQuery 来填充你的选择元素,你可以使用这个:
html
html
<select id="tagging"></select>
js
js
array_of_options = ['Choose Tagging', 'Option A', 'Option B', 'Option C']
$.each(array_of_options, function(i, item) {
if(i==0) { sel_op = 'selected'; dis_op = 'disabled'; } else { sel_op = ''; dis_op = ''; }
$('<option ' + sel_op + ' ' + dis_op + '/>').val(item).html(item).appendTo('#tagging');
})
This will allow the user to see the first option as a disabled heading ('Choose Tagging'), and select all other options.
这将允许用户将第一个选项视为禁用标题(“选择标记”),并选择所有其他选项。
回答by Abdullah Mahi
<select name="dept" id="dept">
<option value =''disabled selected>Select Department</option>
<option value="Computer">Computer</option>
<option value="electronics">Electronics</option>
<option value="aidt">AIDT</option>
<option value="civil">Civil</option>
</select>
use "SELECTED" which option you want to select by defult. thanks
使用“SELECTED”您要默认选择的选项。谢谢