简体   繁体   English

jQuery,如何从多重选择框中获取值

[英]jquery, how to get the values from a multi select box

Does anyone know how to get the selected values from a select box that has multiple set. 有谁知道如何从具有多个设置的选择框中获取选择的值。

thanks 谢谢

<html>
<head>
<script type="text/javascript">
function getSelectedValues()
{
  $("#selectID").?????
}
</script>
</head>

<body>
<select id="selectID" MULTIPLE>
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>
<a href="javascript:getSelectedValues()>press</a>

</body>
</html>
$("#selectID").val();

From the jQuery API documentation on the val() method: val()方法的jQuery API文档中:

The .val() method is primarily used to get the values of form elements. .val()方法主要用于获取表单元素的值。 In the case of <select multiple="multiple"> elements, the .val() method returns an array containing each selected option. 对于<select multiple="multiple">元素, .val()方法将返回一个包含每个选定选项的数组。

You want to use the selected selector 您要使用选择的选择器

http://api.jquery.com/selected-selector/ http://api.jquery.com/selected-selector/

$("#selectID option:selected").each(function () {
            $(this).val(); //this is one of the selected values
          });

$("#selectID").val()返回以逗号分隔的所选值列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM