代码之家  ›  专栏  ›  技术社区  ›  Jakec Pakec

jquery getJson和array[已关闭]

  •  0
  • Jakec Pakec  · 技术社区  · 13 年前

    我在jQuery getJSON和显示结果时遇到了问题。

    jQuery代码

    jQuery(document).ready(function(){
    jQuery.getJSON("gsm.php",{getproducts:'495074, 495061, 495060'},function(response){
        jQuery.each(response, function() {
            jQuery('#item1').html(response.item1);
            jQuery('#item2').html(response.item2);
        });
    
    });
    });
    

    gsm.php JSON响应

    [
    {
        "item1": "test data 1",
        "item2": "test data 1a"
    },
    {
        "item1": "test data 2",
        "item2": "test data a"
    }
    ]
    

    很抱歉,我无法解决其他帖子的问题。

    如果有任何帮助,我将不胜感激。

    谢谢

    3 回复  |  直到 13 年前
        1
  •  1
  •   Lakshmana Kumar    13 年前
    $(document).ready(function() {
    
            $.getJSON("gsm.php", { getproducts: '495074, 495061, 495060' }, function(response) {
                $.each(response, function(key, value) {
                    $('#item1').html(value.item1);
                    $('#item2').html(value.item2);
                });
            });
    
        });
    

    你错过了这个 function(key, value) 并使用value从json中获取数据。

        2
  •  0
  •   Adil Shaikh    13 年前

    试试这个-

    jQuery.each(response, function(index,value) {
            jQuery('#item1').html(value.item1);
            jQuery('#item2').html(value.item2);
    });
    
        3
  •  0
  •   Sushanth --    13 年前

    试试这个

    jQuery.each(response, function() {
        jQuery('#item1').html(this.item1);
        jQuery('#item2').html(this.item2);
    });