【问题标题】:How to override JSON data displayed on table?如何覆盖表格上显示的 JSON 数据?
【发布时间】:2015-10-08 04:50:25
【问题描述】:

我正在尝试制作一个简单的《权力的游戏》评论应用程序。这包含两个输入字段,一个用于季节,另一个用于剧集。我正在尝试将 JSON 数据解析为具有两列的表。第一列将包含图片,第二列将包含评论。图片会随着输入字段的更改而更改,但我无法覆盖第二列的数据。它只是继续附加。

作为初学者和 StackOverflow 的新手,我的声誉很低。所以不能发图。否则它会很清楚。

$(function () {
  $('#btn-got-load').on('click', function () {
    var $season = $('#txt-season').val();
    var $episode = $('#txt-episode').val();
     var $url = "http://www.omdbapi.com/?t=Game%20of%20Thrones&Season=" + $season + "&Episode=" + $episode + "";
        $.ajax({
            type: 'GET',
            url : $url,
            success: function (data) {
                $.each(data, function (key, value) {
                    if (key == "Poster") {
                        $("#image-here").html("<img src='" + value + "'>");
                    }
                    $("#review-here").append("<li>" +key +" : "+ value + "</li>");
                });
                $("#review-here").append( "<br/>");
            }
        });
     });
<h1><u>Game of Thrones review</u></h1>
<label>Season (1-5) : </label> <input type="text" id="txt-season" value="1"/>
<label>Episode (1-10) : </label> <input type="text" id="txt-episode" value="1" />
<input type="button" id="btn-got-load" value="review"/>
<div id="container-div">
    <table>
        <tr>
            <td>
                <span id="image-here"></span>
            </td>

            <td>               
                <ul id="review-here"></ul>                
            </td>
        </tr>
    </table>
</div>

【问题讨论】:

    标签: html ajax json asp.net-web-api


    【解决方案1】:

    您必须在开始添加行之前清除它:

            success: function (data) {
                $("#review-here").html(""); // clear it first
                $.each(data, function (key, value) {
                    if (key == "Poster") {
                        $("#image-here").html("<img src='" + value + "'>");
                    }
                    $("#review-here").append("<li>" +key +" : "+ value + "</li>");
                });
                $("#review-here").append( "<br/>");
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多