本文介绍了如何从< 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(); 

从:
$ b


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:

这篇关于如何从&lt; select multiple = multiple&gt;获取所有选定的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:31