2012-06-02 2 views
0

PHP 파일에서 호출하는 배열을 조작하려고합니다. 그것은 내가 같이 호출하면이이 전체 배열을 보여줍니다 내 jQuery를jQuery와 PHP에서 배열 사용

$(document).ready(function() { 
function get() { 
    var postdata = $('#scu').val(); 
    $.post('data.php', {scu:postdata}, function(output) { 
     $('#output').append(output); 
    }); 
    } 
$('#scu').keyup(get); 
}); 
//#scu is a textbox 
//#output is an empty div 

입니다

[{ 
    "description": "Intel H67 Socket 1155 Motherboard", 
    "price": "144.99"}, 
{ 
    "description": "Thermaltake WO319RU 850 Watt PSU Modular", 
    "price": "169.99"}, 
{ 
    "description": "LG GH24NS70 SATA DVD Burner", 
    "price": "39.99"}, 
{ 
    "description": "eVGA GTX 480 1536MB GDDR5", 
    "price": "299.99"}] 

전송; 내가 output[0]처럼 호출하면 내가 output[0].pric 전자처럼 호출하면 난 내가 훌륭한 작품 unknown' error.

답변

0
$.post('data.php', {scu:postdata}, function(output) { 
     // looping over output 
     $.each(output, function(index, obj) { 
     // obj contains each object eg. 
     // { 
     // "description": "Intel H67 Socket 1155 Motherboard", 
     // "price": "144.99" 
     // } 

     console.log(obj.description); 
     console.log(obj.price); 
     // to append to textbox do something like this 
     $('#scu').append(obj.description); // and so on 
    }); 
}, 'json'); // you need to put dataType here as json, as output is json 
      // you don't need $.parseJSON, because dataType json will 
      // parse it for you 
+0

감사를 얻을 [;를 얻을 수 있습니다. – user1114060

0
$(document).ready(function() { 
function get() { 
    var postdata = $('#scu').val(); 
    $.post('data.php', {scu:postdata}, function(output) { 
     var objects = jQuery.parseJSON(output); // obj will contain the array of objects you need 
     alert(objects[0].price); 
     $('#output').append(output); 
    }); 
    } 
$('#scu').keyup(get); 
}); 
+0

끝내 주셔서 감사합니다. – user1114060