Html html选择元素默认不在选项中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14299017/
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
html select element with default not in the options
提问by Yahya KACEM
Possible Duplicate:
<select> placeholder
可能重复:
<select> 占位符
I want to do something like this:
我想做这样的事情:
<select>
<option selected="selected">Choose option</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
here's the jsfiddle.
now I want the first option the selected one with the content "Choose option" to not be visible in the dropdown. how can i do that, if that's possible with a select element or should i do a custom thing. Thanks in advance.
现在我希望第一个选项是在下拉列表中不可见的带有“选择选项”内容的选定选项。如果可以使用 select 元素或者我应该做一个自定义的事情,我该怎么做。提前致谢。
采纳答案by philipproplesch
You could remove the selected element as soon as you open the list:
您可以在打开列表后立即删除所选元素:
$('select').click(function () {
$('option[selected="selected"]', this).remove();
});
I am not aware of any native HTML/HTML5 solution.
我不知道任何原生 HTML/HTML5 解决方案。
回答by Christofer Eliasson
I believe the closest you can get with a plain HTML-solution is to disable that option:
我相信使用纯 HTML 解决方案最接近的是禁用该选项:
<select>
<option selected="selected" disabled="disabled">Choose option</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
The element will still appear in the the list, but it will be grayed out and it will not be possible to select it from the dropdown after opening it.
该元素仍会出现在列表中,但它会变灰,并且在打开后无法从下拉列表中选择它。
回答by Roy
<select>
<option selected="selected" disabled="disabled">Choose option</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
This will show "choose option" before selecting, but will not allow the user to select it.
这将在选择之前显示“选择选项”,但不允许用户选择它。