[英]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.