RayLoveless
RayLoveless

Reputation: 21108

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>

Upvotes: 1

Views: 2487

Answers (3)

Joseph
Joseph

Reputation: 25523

You want to use the selected selector

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

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

Upvotes: 2

Marcus Leon
Marcus Leon

Reputation: 56699

$("#selectID").val() returns a comma delimited list of selected values.

Upvotes: 1

John
John

Reputation: 9456

$("#selectID").val();

From the jQuery API documentation on the val() method:

The .val() method is primarily used to get the values of form elements. In the case of <select multiple="multiple"> elements, the .val() method returns an array containing each selected option.

Upvotes: 9

Related Questions