简体   繁体   English

HTML <select>元素的默认selectedIndex是否可以为-1而不运行页面范围的JavaScript?

[英]Can an HTML <select> element have a default selectedIndex of -1 without running page-wide javascript?

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. 为此,我将组合框控件封装到.NET服务器端组件中以实现可重用性。 Some pages may have a combobox on them, some may not. 有些页面可能有一个组合框,有些可能没有。

At the core, this combobox contains a textbox and a "dropdown" aligned appropriately with CSS. 在核心,这个组合框包含一个文本框和一个与CSS适当对齐的“下拉列表”。 Part of the HTML rendered is simply: 部分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. 但我不想对页面上的每个元素运行javascript。 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. 我意识到我可以使用jQuery来选择CSS类,但大多数页面上都没有select元素,什么都不会发生。

Is there a way to set selectedIndex that's encapsulated in the tag itself? 有没有办法设置封装在标签本身的selectedIndex? Something like: 就像是:

<select selectedIndex="-1">...

or 要么

<select onload="this.selectedIndex = -1">...

?

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>

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>

以下是select的一些文档: http//www.w3.org/TR/html4/interact/forms.html#h-17.6

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>

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

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