admin管理员组文章数量:1391969
From MDN:
defaultSelected - Optional
A Boolean that sets the selected attribute value, i.e. so that this will be the default value selected in the element when the page is first loaded. If this is not specified, a default value of false is used. Note that a value of true does not set the option to selected if it is not already selected.selected - Optional
A Boolean that sets the option's selected state; the default is false (not selected). If omitted, even if the defaultSelected argument is true, the option is not selected.
This is very unclear to me. defaultSelected
determines if "this will be the default value selected", but one sentence later it says "a value of true does not set the option to selected if it is not already selected". So... it doesn't set it to be selected? It only sets it to selected if it's already selected?
selected
claims also to set the selected state. If it's omitted, regardless of defaultSelected
, the option is not selected. So... defaultSelected
does nothing?
What's going on here?
From MDN:
defaultSelected - Optional
A Boolean that sets the selected attribute value, i.e. so that this will be the default value selected in the element when the page is first loaded. If this is not specified, a default value of false is used. Note that a value of true does not set the option to selected if it is not already selected.selected - Optional
A Boolean that sets the option's selected state; the default is false (not selected). If omitted, even if the defaultSelected argument is true, the option is not selected.
This is very unclear to me. defaultSelected
determines if "this will be the default value selected", but one sentence later it says "a value of true does not set the option to selected if it is not already selected". So... it doesn't set it to be selected? It only sets it to selected if it's already selected?
selected
claims also to set the selected state. If it's omitted, regardless of defaultSelected
, the option is not selected. So... defaultSelected
does nothing?
What's going on here?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 28, 2017 at 17:35 SinjaiSinjai 1,2771 gold badge19 silver badges36 bronze badges 02 Answers
Reset to default 5The defaultSelected
property represents the initial (default) selection state of an element and corresponds to the selected HTML attribute. The selected
property represents the current selection state and that is what a user actually sees selected in the browser.
When a user selects an option, only the selected
property is changed, while the attribute and the defaultSelected
property stay the same (they can still be changed programmatically though). You can use this, for instance, to reset some settings to defaults.
So I'd say you would generally use either
new Option(text, value, true, true)
or
new Option(text, value, false, false)
Setting different values for the 3rd and the 4th parameters means you create an option which is the default value, but is not currently selected, or vice versa.
I find the docs rather confusing as well, but I guess that
defaultSelected - <...> Note that a value of true does not set the option to selected if it is not already selected
means just that - if you set the defaultSelected
to true, it does not mean that the option will actually be selected, it just sets the option as the default selected value. For it to be selected you have to set the selected
property as well.
Also, when you add the selected
attribute in HTML, the corresponding DOM element gets both defaultSelected
and selected
properties set to true, so both initial and current selected values are the same.
Here is a useful answer for a related question about attributes and properties of DOM elements: https://stackoverflow./a/6004028/1630480
seems to me that defaultSelected = true is meant to be a decision on behalf of the user. I noticed that the 'option' having true setting for defaultSelected has the "selected" html attribute in the rendered html, yet its not the selected item in the list. beats me why but i understand the intention of it somewhat.
本文标签:
版权声明:本文标题:javascript - What's the difference between 'selected' and 'defaultSelected' when constru 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744699553a2620482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论