[英]In a HTML select element, why the last option that has selected=false is selected?
我有一个包含option
元素的select
元素。 有些选项的属性selected
设置为 false,有些则根本没有设置。
<select> <option selected=false>1</option> <option selected=false>2</option> <option>3</option> <option selected=false>4</option> <option>5</option> <option>6</option> </select>
似乎当没有元素selected
设置为true
时, selected
设置为false
的最后一个元素是创建元素时默认选择的元素。
这种行为不直观,在这种情况下,我希望selected
未定义的第一个option
,并且绝对不是明确未选择的选项。
是什么导致了这种行为? 它是否在任何地方定义?
问题是disabled
是一个布尔属性。 当它存在时,它处于打开状态,并且它具有什么值并不重要,因此具有selected
属性的最后一个元素被选中。 即使你写false
,它仍然被解释为true
(参见What values can appear in the "selected" attribute of the "option" tag? )。
为什么使用selected False,你能解释一下目的吗? 试试看。
<select>
<option disabled>1</option>
<option disabled>2</option>
<option>3</option>
<option disabled>4</option>
<option>5</option>
<option>6</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.