Is there a standard client behaviour for submitting an empty select multiple?

浪尽此生 提交于 2019-12-23 18:29:37

问题


http://www.w3.org/TR/html401/interact/forms.html#edef-SELECT

Doesn't specify how the client should behave if the submitted select multiple is empty. Does anyone know whether there is a standard for that ?


回答1:


Sadly, I do not have the time to test this, but I hope the following can help you...

If I am not mistaken, the first element is selected by default when the page is loaded. Therefore, create a very simple HTML file with a form and a select that has the multiple attribute active and with a few options. Then, open the file to see what happens in a few browsers such as Chrome, Firefox, Internet Explorer, Opera and Safari.

Another thing would be to look what HTML5 says about this here: http://dev.w3.org/html5/spec/Overview.html#the-select-element.

UPDATE

The W3C HTML 4.01 standard states this (scroll a bit down in the link you provided):

17.6.1 Pre-selected options

Zero or more choices may be pre-selected for the user. User agents should determine which choices are pre-selected as follows:

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 elements. Since user agent behavior differs, authors should ensure that each menu includes a default pre-selected OPTION.

If one OPTION element has the selected attribute set, it should be pre-selected. If the SELECT element has the multiple attribute set and more than one OPTION element has the selected attribute set, they should all be pre-selected. It is considered an error if more than one OPTION element has the selected attribute set and the SELECT element does not have the multiple attribute set. User agents may vary in how they handle this error, but should not pre-select more than one choice.




回答2:


Unfortunately, @Alerty is mistaken. If the multiple attribute is present on the select element but none of the option elements have the selected attribute, then no element is selected by default. (Tested on IE 9, FF 5, Chrome 13, and Safari 5 on Win 7.)

So, what should the web browser do if the user doesn't select anything yet submits the form? It should do exactly what you would expect: Submit no selection. And that's exactly what the current version of the HTML5 spec says:

"The multiple attribute is a boolean attribute. If the attribute is present, then the select element represents a control for selecting zero or more options from the list of options."

http://dev.w3.org/html5/spec/Overview.html#the-select-element



来源:https://stackoverflow.com/questions/5242944/is-there-a-standard-client-behaviour-for-submitting-an-empty-select-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!