javascript - 如何使用 Ajax 每个函数和动态数量的 DIV 标签

标签 javascript jquery ajax

我的 code-igniter mysql JSON 响应内容大量的电子邮件地址。我应该如何为每个电子邮件地址添加每个 div 标签。现在只显示最后一个电子邮件地址。

$(document).ready(function() {
  $('#option1').click(function() {
    $.ajax({
      url: '<?php echo base_url(); ?>' + 'main/studentdetails',
      dataType: "JSON",
      type: "POST",
      success: function(retdata) {
        $.each(retdata, function(i, item) { //i = number of records
          $(".target").html(retdata[i].email);
        });
      }
    });
  });
});
<body>
  <input type="button" id="option1">
  <div class='target'>
  </div>
</body>

最佳答案

您需要使用 append() 而不是 html() ,否则它将替换之前的值并显示最后一个值。

$(".target").append(item.email);

关于javascript - 如何使用 Ajax 每个函数和动态数量的 DIV 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36100381/

相关文章:

javascript - Angular js ng-重复重复项

javascript - 将值传递给 jQuery 成功函数中的 Bootstrap 模式

php - 将jquery mobile和php mysql转为apk文件

javascript - RegExp 不是一个函数

javascript - 事件与自定义事件

javascript - Kendo Grid 在 Change 事件上获取行和列索引

javascript - 需要事件处理程序时切换类问题

jquery - 使用低调的 Ajax 举办全局事件

javascript - 通过 AJAX 无限数据流来更新网页中的字段?

mysql - 用户界面自动完成 : Make multiple ajax requests or load all data at once for a list of locations in a city?