簡體   English   中英

在 HTML select 元素中,為什么選擇了最后一個 selected=false 的選項?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM