如何使用ajax附加多个值

时间:2019-01-22 14:38:46

标签: javascript jquery html ajax

在响应中,我有多个项目,我想在h2类的响应中附加每个项目。我的代码只是循环遍历一个类autocomplete=nope

中的所有项目

这是我的尝试:

<h2 id="test">
$(function() {

  var $test = $('#test').one();

  $.ajax({

    type: 'GET',
    url: 'test.json',
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function(tests) {
      <!-- <!-- console.log(gares) --> -->

      $.each(tests, function(index, item) {
        $tests.append('<h2>' + item[1] + '</h2>');
      });
    }

  });

});

1 个答案:

答案 0 :(得分:0)

仅当您使用id而不是class时,它才会遍历1个项目,请尝试将HTML更改为:

<h2 class="test"></h2>
<h2 class="test"></h2>

然后您的JS应该看起来像这样:

var $test = $('.test')

我建议使用reading this