gpt4 book ai didi

javascript - 找出多个下拉菜单的值,仅检查第一个选项

转载 作者:行者123 更新时间:2023-11-28 18:33:35 25 4
gpt4 key购买 nike

这是我的代码:

    <select id="gender" style="width: 125px;">
<option>Select</option>
<option>Female</option>
<option>Male</option>
<option>Other</option></select>

<select id="car" style="width: 125px;">
<option id="2">Select</option>
<option id="2">Sport</option>
<option id="2">Luxury</option>
<option id="2">Muscle</option>
<option id="2">Formula 1</option></select>

问题是当我运行时:

    var gender = document.getElementsByTagName("option")[document.getElementById("gender").selectedIndex].value;
var car = document.getElementsByTagName("option")[document.getElementById("car").selectedIndex].value;

它知道我有两个选择,但只检查第一个选择的第一个选项。因此,当我为每个选项选择第一个选项时:

女性女

而不是女性和运动。

最佳答案

为什么不直接像下面这样尝试

var gender=document.getElementById('gender').value;
var car=document.getElementById('car').value

它只会为您提供选定的值。

关于javascript - 找出多个下拉菜单的值,仅检查第一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37514265/

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