JQuery .each() 메서드


http://api.jquery.com/jquery.each/



Examples:

Example: Iterates through the array displaying each number as both a word and numeral

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.each demo</title>
<style>
div {
color: blue;
}
div#five {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>
<div id="five"></div>
<script>
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
jQuery.each( arr, function( i, val ) {
$( "#" + val ).text( "Mine is " + val + "." );
// Will stop running after "three"
return ( val !== "three" );
});
jQuery.each( obj, function( i, val ) {
$( "#" + i ).append( document.createTextNode( " - " + val ) );
});
</script>
</body>
</html>



$("#listBnt").click(function viewAjax() {
$('#jsonlist').empty();
console.log("리스트 버튼 들어옴");
$.ajax({
type: "GET",
url: "/testajax/main/users/",
dataType: "json",
success: function (response) {
console.log("불러오기 성공");
// each 함수 $(불러온데이터).each(fucntion (인덱스, 불러온데이터각각의 값)){}
$(response).each(function (i,data ) {
var d = "<table class='table table-striped'>";
d += "<th class='tid'>" + data.id + "</th>";
d += "<th class='tname'>" + data.name + "</th>";
d += "<th class='temail'>" + data.email + "</th>";
d += "<th class='tage'>" + data.age + "</th>";
d += "</table>";
$("#jsonlist").append(d);
})
},
error: function (e) {
console.log("error");
console.log("error 정보 : " + e.status);
}
});
});


'Programming > jQuery' 카테고리의 다른 글

[jquery] 패스워드 에러 표현 innerHtml  (0) 2017.03.27
[ajax]multipart form 전송  (0) 2016.10.13
[jQuery] function 정의  (0) 2015.07.02

+ Recent posts