首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JavaScript / AJAX将对象插入到HTML表中

如何使用JavaScript / AJAX将对象插入到HTML表中
EN

Stack Overflow用户
提问于 2018-08-09 01:46:24
回答 2查看 36关注 0票数 0

我试图创建一个网站,通过搜索艺术家的名字从音乐论坛中获取信息,并按流行度、曲目数量和发布日期填充艺术家的5张顶级专辑。信息是正确的从网站拉取,但当我去创建和HTML表没有显示任何信息。搜索按钮功能正常,它调用所有正确的信息,可以帮助提供一个解决方案,其中我可以从数组中提取信息,并在HTML中填充/创建表?下面是我目前正在使用的代码。

代码语言:javascript
复制
function searchClicked() {
   var artist_name = document.getElementById("artistName").value;

   var data = {};
   data.ArtistName = artist_name
    $.ajax({
        type: "POST",
        url: "/Home/SearchAlbum",
        data: JSON.stringify(data),
        success: function (responseData) {
            debugger;
            function writeTable() {
                var html = ''
                for (var i = 0; i < responseData; i++)
                    html += '<tr>';
                for (var j = 0; j < totalCells; j++) {
                    html += '<td>' + array[count] + '</td>';
                    count++;
                }
                html += '</tr>';
            }
            
            $('#body').append(html);
            
            count = 0;
            
        },
                 contentType: "application/json; charset=utf-8",
   })
代码语言:javascript
复制
table {
    width: 100%
}

table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}

th, td {
    padding: 5px;
    text-align: left;
}

table#artistName tr:nth-child(even) {
    background-color: aquamarine
}

table#artistName tr:nth-child(odd) {
    background-color: aquamarine
}

table#artistName th {
    background-color: black;
    color: white;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="jumbotron">
   <p> Enter Artist Name:</p>
   <input id="artistName" type="text" />
   <button id="seachButton" onclick="searchClicked()">seach</button>
 </div>


<table id="ALbumInfo">
  <thead>
     <tr>
      <th>Album Name </th>
      <th>Release Date</th>
      <th>Popularity</th>
      <th>Number of Tracks</th>
     </tr>
  </thead>
  <tbody></tbody>
</table>

我真的很想知道这里出了什么问题。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51752725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档