The jquery map method is used as an example


JQuery. Map (array, callback(elementOfArray, indexInArray))

Returns: an Array

Feel the jquery map method is very useful, I would like to share with you.

Method replaces an array or a single object with new content. Example of an application: get a set of checkbox values, separated by English commas, and splice them.


<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script>
$(function(){
$("input").click(function(){
var str = $(":checked").map(function(){
return this.value;
}).get().join(",")
$("span").html(str);
})
})
</script>
</head>
<body>
<input type="checkbox" value="a"/>a
<input type="checkbox" value="b"/>b
<input type="checkbox" value="c"/>c
<br/>
 Results: <span/>
</body>
</html>

In CSDN code editor, dare not flatter really, I’m jsFiddle created the above example, attached to the link: http://jsfiddle.net/dV8eh/.