HTML <select> 元素可以在不运行页面范围的 javascript 的情况下具有 -1 的默认 selectedIndex 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7648922/
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
Can an HTML <select> element have a default selectedIndex of -1 without running page-wide javascript?
提问by Mike
I have several places in my website where a "combobox" style control is needed. To do this, I've encapsulated the combobox control into a .NET server-side component for re-usability. Some pages may have a combobox on them, some may not.
我的网站中有几个地方需要“组合框”样式控件。为此,我将组合框控件封装到 .NET 服务器端组件中以实现重用。有些页面上可能有组合框,有些可能没有。
At the core, this combobox contains a textbox and a "dropdown" aligned appropriately with CSS. Part of the HTML rendered is simply:
在核心,这个组合框包含一个文本框和一个与 CSS 适当对齐的“下拉”。呈现的部分 HTML 很简单:
<select>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
The dropdown must start off empty, and changing to any value (including "Option 1") must trigger the onchange event.
下拉列表必须从空开始,更改为任何值(包括“选项 1”)必须触发 onchange 事件。
I accomplish this by doing something like:
我通过执行以下操作来完成此操作:
document.getElementById("theSelectElement").selectedIndex = -1;
but I'd prefer not to have to run javascript against each element on the page. I realize that I could use jQuery to select against a CSS class, but most pages won't have a select element on it and nothing will happen.
但我不想对页面上的每个元素运行 javascript。我意识到我可以使用 jQuery 来选择 CSS 类,但大多数页面上不会有 select 元素,什么也不会发生。
Is there a way to set selectedIndex that's encapsulated in the tag itself? Something like:
有没有办法设置封装在标签本身中的 selectedIndex ?就像是:
<select selectedIndex="-1">...
or
或者
<select onload="this.selectedIndex = -1">...
?
?
回答by Mike
a select is a radio state holder so there is no "non-selected" state
选择是无线电状态持有者,因此没有“非选择”状态
<select name="aaa">
<option value=""></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
btw you can use an empty < option >
顺便说一句,您可以使用空的 < option >
回答by Ghazanfar Mir
You may use the following: By default the first option will be selected automatically
您可以使用以下内容:默认情况下,将自动选择第一个选项
<select name="aaa">
<option value="0">select any option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
回答by ScorNinja
Here is some documentation of select: http://www.w3.org/TR/html4/interact/forms.html#h-17.6
这是一些选择的文档:http: //www.w3.org/TR/html4/interact/forms.html#h-17.6
回答by Widor
If you've wrapped it in your own user control or custom control then you'll need to expose the defaultSelection
property (or whatever you name it) publicly so it can be specified in your server tag.
如果您将它包装在您自己的用户控件或自定义控件中,那么您需要公开公开该defaultSelection
属性(或您命名的任何内容),以便可以在您的服务器标记中指定它。
You talk about doing it in a <select>
tag, but this is the HTML markup - not the server-side markup of <ASP:DropDown ... >
or <myControl:CustomSelect ... >
您谈论在<select>
标签中执行此操作,但这是 HTML 标记 - 不是服务器端标记<ASP:DropDown ... >
或<myControl:CustomSelect ... >
EDIT :If it's pure HTML, then just output the default selection with the selected
property set:
编辑:如果它是纯 HTML,那么只需输出带有selected
属性集的默认选择:
<select>
<option value="1">Top</option>
<option selected="selected" value="-1">Middle (Empty)</option>
<option value="2">End</option>
</select>