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 getS开发者_如何学CelectedValues()
{
$("#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:
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.
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
});
$("#selectID").val()
returns a comma delimited list of selected values.
精彩评论