gpt4 book ai didi

javascript - 如何获取选择的选项ID?

转载 作者:技术小花猫 更新时间:2023-10-29 12:49:00 26 4
gpt4 key购买 nike

从 HTML select 中获取选定的值:

选项[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;

始终检查(或评估为的值)。示例 2 将变量设置为 null(如果没有选择任何内容)。

关于javascript - 如何获取选择的选项ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1904827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com