0

PHP ファイルから呼び出す配列を操作しようとしています。送信します

[{
    "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"}]

これは私のjQueryです

$(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

このように呼び出すと、配列全体が表示されます。のようoutput[0][;呼び出すと、output[0].pric取得できますunknown' error.

4

2 に答える 2

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
于 2012-06-02T04:10:54.520 に答える
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);
});
于 2012-06-02T04:12:28.700 に答える