如何从< select multiple = multiple>获取所有选定的值? [英] How to get all selected values from <select multiple=multiple>?
问题描述
我有一个html,如下所示:
< select id =select-meal-typemultiple =multiple>
< option value =1>早餐< / option>
< option value =2>午餐< / option>
< option value =3>晚餐< / option>
< option value =4> Snacks< / option>
< option value =5>甜点< / option>
< / select>
如何获得用户在javascript中选择的所有值(数组?)?
例如,如果用户选择了Lunch and Snacks,我需要一个{2,4}数组。
<这似乎应该是一个非常简单的任务,但我似乎无法做到。
谢谢。
通常的方法: var values = $('#select-meal-type')。val();
从 docs :
$ b
在
code>元素,< select multiple =multiple>的情况下
.val()
方法返回一个包含每个选定选项的数组;
Seemed odd I couldn't find this one already asked, but here it goes!
I have an html as follows:
<select id="select-meal-type" multiple="multiple">
<option value="1">Breakfast</option>
<option value="2">Lunch</option>
<option value="3">Dinner</option>
<option value="4">Snacks</option>
<option value="5">Dessert</option>
</select>
How do I get all the values(an array?) that the user has selected in javascript?
For example, if the user has selected Lunch and Snacks, I want an array of { 2, 4 }.
This seems like it should be a very simple task but I can't seem to do it.
Thanks.
The usual way:
var values = $('#select-meal-type').val();
From the docs:
In the case of
<select multiple="multiple">
elements, the.val()
method returns an array containing each selected option;
这篇关于如何从< select multiple = multiple>获取所有选定的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!