[英]How to get selected option ID?
要從HTML獲取選定的值,請選擇:
options[selectedIndex].value
如果我想獲取所選選項的"id"
怎么辦?
無需過多假設(即select是有效的SELECT元素),
var options = select.options;
var id = options[options.selectedIndex].id;
var value = options[options.selectedIndex].value;
要么,
var options = select.options;
var value = (options.selectedIndex != -1) ? options[selectedIndex].value : null;
var id = (options.selectedIndex != -1) ? options[selectedIndex].id : null;
始終檢查虛假性 (或評估為false的值)。 示例2將變量設置為null (如果未選擇任何內容)。
就像這樣簡單:
options[selectedIndex].id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.