循环通过javascript对象并输出相同的元素

时间:2016-09-21 04:51:34

标签: javascript jquery html json ajax

 $(document).on('mouseenter', '.grid-img-hover', function() {
 var container = $(this);
 var jobId = container.parent().find('.title-wrap-hidden').text();

    $.ajax({
            url: 'db_client_job_name_lookup.php',
            type: 'POST',
            dataType: 'json',
            data:{
               jobId: jobId
                 },
               success: function(data) {
               //console.log(data.client_name[0][0][0]['name']);
                container.find('.title-wrap').html('<p class="job-name">'+ data.portfolio_item_name  +
                     '</p><p class="client-name">'+ data.client_name[0][0][0]['name']  +'</p>');
                  //  container.find('.title-wrap').html('<p class="client-name">'+ data.result.client_name  +'</p>');
                   },
                   error: function(jqXHR, textStatus, errorThrown) {
                     // error
                     alert(errorThrown);
                   }
                  });

                  });

您好,我有这行代码,我想循环遍历data.client_name[0][0][0]['name']并在同一<p class="client-name"></p>标记中输出其值。索引会在data.client的中间改变,如此(data.client_name[0][$index][0]['name'])。我怎么能这样做?感谢。

2 个答案:

答案 0 :(得分:0)

这是你的意思吗?

for (i = 0; i < data.client_name[0].length; i++) { 
    container.find('.title-wrap').html('<p class="job-name">'+ data.portfolio_item_name +
'</p><p class="client-name">'+ data.client_name[0][i][0]['name']  +'</p>');
}

答案 1 :(得分:0)

$(document).on('mouseenter', '.grid-img-hover', function() {
                      var container = $(this);
                      var jobId = container.parent().find('.title-wrap-hidden').text();

                      $.ajax({
                       url: 'db_client_job_name_lookup.php',
                       type: 'POST',
                       dataType: 'json',
                       data: {
                         jobId: jobId
                       },
                       success: function(data) {
                         var client_names_text;
                         var clients_array = [];
                         function make_clients(){
                           for (var i = 0; i < data.client_name[0].length; i++) {
                                clients_array.push(data.client_name[0][i][0]['name']);
                           }
                           client_names_text = clients_array.join(',');
                           return client_names_text;
                         }
                         //console.log(data.client_name[0][0][0]['name']);
                         container.find('.title-wrap').html('<p class="job-name">'+ data.portfolio_item_name  +
                         '</p><p class="client-name">'+ make_clients()  +'</p>');
                      //  container.find('.title-wrap').html('<p class="client-name">'+ data.result.client_name  +'</p>');
                       },
                       error: function(jqXHR, textStatus, errorThrown) {
                         // error
                         alert(errorThrown);
                       }
                      });

                      });

好的,我通过创建一个功能来完成繁重的工作,这里有工作代码块。