开发者

alert all selected value in SELECT - JQUERY

how can i get all the selected value in tag SELECT?

<select id="list" multiple=multiple>
    <option value="1" selected="selected">one </option>
    <option value="2" selected="selected">two </option>
    <option value="3">three </option>
</select>

for example use foreach as php???

live: http://jsfiddle.net/FMF7c/1/

how can i show with alert in this example one and two? I would like 开发者_运维技巧use JQUERY


The val()[docs] method will return an Array for a multiple <select>.

alert( $('#list').val() ); // to show both
$.each( $('#list').val(), function(i,v) { alert( v ); }); // to show individual

Example: http://jsfiddle.net/FdMMK/


$("#list").find("option:selected").each(function() { alert($(this).val()); });

http://jsfiddle.net/yjL9n/1/


I guess just "alerting" those values is not really what you want. To .map() those values into an Array, we can use the below:

var values = $('#list option:selected').map(function(_, node) {
    return node.value; // or node.textContent || node.text
}).get();

alert( values );

http://jsfiddle.net/FMF7c/4/


Updated the JSFiddle - http://jsfiddle.net/FMF7c/5/ I've used simple JavaScript to loop through all the options in the select element, and add any of which are selected to an array.

Hah, looks like you didn't need my help after all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜