2011-05-06 58 views
-3

任何人都可以expalin我關於$ .each()?

+4

哦,看有文檔:http://api.jquery.com/jQuery.each/也許你想詳細說明你在* json中的含義。 – 2011-05-06 09:22:25

回答

3

假設有以下數組對象:

var data = [ { 'id': '1', 'name': 'foo' }, { 'id': 1, 'name': 'bar' } ]; 

就可以通過它的元素循環:

$.each(data, function(index, value) { 
    // this will be executed for each element of the array and here 
    // you can use value.id and value.name which are the two properties 
    // defined for each object in the array 
}); 

但通常這JSON來自服務器響應AJAX請求。所以在這個請求的成功回調,你可以通過服務器返回的數組元素的循環:

$.getJSON('/foo.cgi', function(data) { 
    $.each(data, function(index, value) { 
     alert(value.name); 
    }); 
}); 
相關問題