选择没有选定选项的元素 [英] Select element with no selected options

查看:101
本文介绍了选择没有选定选项的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


是否可以创建< select>元素没有选择的选项?我已经尝试将selectedIndex属性设置为-1,但据我所知

告诉这只适用于< select multiple>元素。


我错过了一些明显的东西吗?


此致,

TJ Walls

博士候选人 - 石溪大学

Hello All,

Is it possible to create a <select> element with no selected options? I
have tried setting the selectedIndex attribute to -1, but as far as I can
tell this only works for <select multiple> elements.

Am I missing something obvious?

Sincerely,
TJ Walls
Ph.D. Candidate - Stony Brook University

推荐答案

2004年10月27日星期三07:47:47 -0400,TJ Walls

< TJ ***** @ mindspring.nospam.com>写道:
On Wed, 27 Oct 2004 07:47:47 -0400, TJ Walls
<tj*****@mindspring.nospam.com> wrote:
是否可以创建一个< select>元素没有选择的选项?


通过不给任何OPTION元素选择属性,用户代理最初没有初始值可能是
。但是,无论是什么原因,大多数都会选择第一个。

我尝试将selectedIndex属性设置为-1,但据我所知这只适用于< select multiple>元素。


适用于IE,Opera和Firefox。

我错过了一些明显的东西吗?
Is it possible to create a <select> element with no selected options?
By not giving any OPTION elements the selected attribute, it is possible
for a user agent to initially have no initial value. However, for whatever
reason, most do select the first.
I have tried setting the selectedIndex attribute to -1, but as far as I
can tell this only works for <select multiple> elements.
Works for me in IE, Opera, and Firefox.
Am I missing something obvious?




也许吧。你究竟想要实现什么目标?


迈克


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



Perhaps. What exactly are you trying to acheive?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


TJ Walls写道:
TJ Walls wrote:
大家好,

可以创建一个< select>元素没有选择的选项?我已经尝试将selectedIndex属性设置为-1,但据我所知
告诉这只适用于< select multiple>元素。

我错过了一些明显的东西吗?


如果你愿意,你可以,但这将毫无意义。


查看HTML规范:

如果没有OPTION元素具有所选属性集,则用户代理行为
用于选择最初选择哪个选项是未定义的。
Hello All,

Is it possible to create a <select> element with no selected options? I
have tried setting the selectedIndex attribute to -1, but as far as I can
tell this only works for <select multiple> elements.

Am I missing something obvious?

You can if you like, but it would be pointless.

Take a look at the HTML specs:
If no OPTION element has the selected attribute set, user agent behavior
for choosing which option is initially selected is undefined.




如何添加< OPTION selected> < / OPTION>开始你的选择

清单?


-

Philip Ronan
ph *********** @ virgin.net

(请删除如果通过电子邮件回复,则为z。



How about adding <OPTION selected> </OPTION> to the start of your options
list?

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)


TJ Walls写道:
TJ Walls wrote:
大家好,

可以创建一个< select>元素没有选择的选项?我


未选择任何选项列表的用户代理行为是

undefined,所以如果你做到了我建议结果将是

在不同的浏览器中无法预测。


HTML规范说你应该确保选择一个选项

- 如果没有选择选择 ;属性,然后第一个选项

应该被选中。

已经尝试将selectedIndex属性设置为-1,但据我所知
告诉这只有作用for< select multiple>元素。


这是一个坏主意,如上所述。

我错过了一些明显的东西吗?
Hello All,

Is it possible to create a <select> element with no selected options? I
User agent behaviour for an option list with nothing selected is
undefined, so if you do achieve it I suggest the results will be
unpredictable across different browsers.

The HTML specification says that you should ensure that one option is
selected - if none have the "selected" attribute, then the first option
should be selected.
have tried setting the selectedIndex attribute to -1, but as far as I can
tell this only works for <select multiple> elements.

This is a bad idea, as above.
Am I missing something obvious?




是的,w3c HTML规范明确表示不要这样做:


< URL:http://www.w3.org/TR/REC-html40/ interact / forms.html>

* 17.6.1)*

"如果没有OPTION元素设置了所选属性,则用户代理

选择最初选择哪个选项的行为是

undefined。注意。由于现有的实现以不同的方式处理这种情况,因此当前规范与RFC 1866(

[RFC1866]第8.1.3节)不同,后者指出:


初始状态选择了第一个选项,除非在任何< OPTION>上有SELECTED

属性。元素。


由于用户代理行为不同,作者应确保每个

菜单都包含默认的预选OPTION。

弗雷德。



Yes, the w3c HTML spec explicitly says don''t do it:

<URL:http://www.w3.org/TR/REC-html40/interact/forms.html>
*17.6.1)*
"If no OPTION element has the selected attribute set, user agent
behavior for choosing which option is initially selected is
undefined. Note. Since existing implementations handle this case
differently, the current specification differs from RFC 1866 (
[RFC1866] section 8.1.3), which states:

The initial state has the first option selected, unless a SELECTED
attribute is present on any of the <OPTION> elements.

Since user agent behavior differs, authors should ensure that each
menu includes a default pre-selected OPTION ."
Fred.


这篇关于选择没有选定选项的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆