获取两个下拉框选中的隐藏值
时间: 2023-10-06 22:10:24 浏览: 49
假设你有两个下拉框,分别是id为`select1`和`select2`,并且每个下拉框的选项都有对应的隐藏值`value1`和`value2`,那么你可以通过以下的JavaScript代码获取选中的隐藏值:
```javascript
// 获取选中的下拉框
const select1 = document.getElementById('select1');
const select2 = document.getElementById('select2');
// 获取选中的选项的下标
const selectedIndex1 = select1.selectedIndex;
const selectedIndex2 = select2.selectedIndex;
// 获取选中的选项的隐藏值
const selectedValue1 = select1.options[selectedIndex1].getAttribute('value');
const selectedValue2 = select2.options[selectedIndex2].getAttribute('value');
```
这样,`selectedValue1`和`selectedValue2`就分别保存了两个下拉框选中的隐藏值。
相关问题
jquery如何获取一个下拉框选中的值
可以使用 jQuery 中的 `val()` 方法来获取下拉框选中的值。示例如下:
```html
<select id="mySelect">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
```
```javascript
// 获取选中的值
var selectedValue = $('#mySelect').val();
console.log(selectedValue); // 输出选中的值
```
注意:`val()` 方法只能获取第一个选中的值。如果需要获取多个选中的值,可以使用 `$('select option:selected')` 来获取选中的选项,然后遍历获取选项的值。
python获取tkinter下拉框选中的值
要获取tkinter下拉框(Combobox)选中的值,可以使用combobox的get()方法,例如:
```python
from tkinter import *
from tkinter.ttk import Combobox
root = Tk()
# 创建下拉框
combo = Combobox(root)
combo["values"] = ["Python", "Java", "C++", "JavaScript"]
combo.pack()
def get_selected_value():
# 获取选中的值
selected_value = combo.get()
print(selected_value)
# 创建获取选中值的按钮
btn = Button(root, text="获取选中值", command=get_selected_value)
btn.pack()
root.mainloop()
```
在上面的示例中,我们创建了一个下拉框,选项为Python、Java、C++和JavaScript,然后创建了一个按钮,点击按钮后可以获取选中的值并打印出来。