Jquery Each Json Object

前端 未结 3 495
挽巷
挽巷 2021-02-04 01:11

I have a result set returning from a service that gives me the following json

{
    \"ThreadCount\":61,
    \"GroupList\":[
        {\"userName\":\"KLT\",\"count         


        
相关标签:
3条回答
  • 2021-02-04 01:51

    When I've used $.each() I have used a function(i, item), where i is an integer indicating index, and item is the actual object. That is how the documentation shows it being done -- the method is described as function callback(indexInArray, valueOfElement).

    0 讨论(0)
  • 2021-02-04 01:59

    Inside the function provided to each, this refers to the current element. Try this:

    $.each(result.GroupList, function() {
        $('#myTable > tbody').append(
            '<tr><td>'
            + this.userName
            + '</td><td>'
            + this.count +
            '</td></tr>'
        );
    });
    

    If that doesn't work for you, it may have something to do with this: $('#myTable > tbody'), considering that there is no tbody element. I believe that Internet Explorer will automatically create one but the other browsers won't. Check $.support.tbody to see if the browser does that for you.

    0 讨论(0)
  • 2021-02-04 01:59

    I noticed your table does not actually have a tbody element. This could be part of your issue.

    $('#myTable > tbody').append.....
    
    <table id="myTable" border="2" cellpadding="3" cellspacing="3">
    </table>
    

    I would also suggest that you create a string in your $.each() loop and then do the following after your each loop:

    $('#myTable > tbody').html(string);
    

    This will reduce the overhead of appending each time you iterate over the array.

    0 讨论(0)
提交回复
热议问题