在追加时,更改值并删除旧值

时间:2015-09-09 09:03:59

标签: javascript jquery ajax

我有以下JSON:

[{"page_title":"SaSaZu Club","page_url":"sasazu"},{"page_title":"Karlovy l\u00e1zn?","page_url":"karlovy-lazne"},{"page_title":"James Dean","page_url":"james-dean"},{"page_title":"Club Lavka","page_url":"club-lavka"},{"page_title":"TRETTER'S Cocktail Bar.","page_url":"tretters-cocktail-bar"}]

我的Ajax将TR添加到表中:

    $(document).ready(function () {
    $("#category").change(function () {
        var category = $('#category option:selected').val();

        $.ajax({
            type: "GET",
            url: '/administrator/page/get_page_by_category',
            data: {'category': category},
            async: true, //Hey browser! run the code
            //if we prased data run:
            success: function (data) {
                var obj = JSON.parse(data);
                var trHTML = '';
                $.each(obj, function (i, item) {
                    trHTML += '<tr><td>' + item.page_title + '</td><td>' + item.page_url + '</td></tr>';
                });
                $('#records_table').append(trHTML);
            }
        });
    });
});

工作正常,但每次我使用$category并更改值。新值只是在现有的顶部,我想在那里单独显示每个TR ..我想我应该只使用其他东西然后&#34;追加?&#34;

1 个答案:

答案 0 :(得分:3)

我认为你需要覆盖内容,因此使用.html(htmlString)代替.append()

$('#records_table').html(trHTML);