0

我得到了我想要的结果,当我按下按钮时,它会删除我不想要的整个表格。

这是我的jQuery

$(document).ready(function() {
$('#button1').click(function(event) {
    $('#list').empty();
    $.ajax({
        url : 'http://98.199.64.63/api/eric_api.php?q=series',
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            var table = $("#list");
            //table.html('');
            $.each(data, function(idx, elem) {
                table.append("<tr><td>" + elem.id + "</td>" + "<td>" + elem.user + 
                "</td>" + "<td>" + elem.email + "</td>" + "<td>" + elem.summary + "</td>" +
                "<td>" + elem.due_date + "</td>" + "<td>" + elem.problem_type + "</td>"+
                "<td>" + elem.status + "</td></tr>");
            });
        },
        error : function() {
            alert('There was an error');
        }
    });
});
});

这是我的html

<!DOCTYPE html>


<html>
<head>
    <title></title>
    <script src="js/jquery.js"></script>
    <script src="js/api_calls.js"></script>
    <link rel="stylesheet" href="css/normalizer.css" />
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <div>
        <table id="list">
            <tr>
                <th>ID</th>
                <th>User</th>
                <th>Email</th>
                <th>Summary</th>
                <th>Due Date</th>
                <th>Problem Type</th>
                <th>Status</th>
            </tr>
        </table>
    </div>
    <div>
        <button id="button1">Get Data</button>
    </div>
</body>
 </html>

我只想刷新 td 标签中的数据,而不是 th 标签。任何帮助谢谢

4

2 回答 2

1

选项1

您始终可以在第一个 TR 之后删除任何 TR,然后在表中附加新数据。

var table = $("#list");
table.find('tr:gt(0)').remove();

$.each(data, function(idx, elem) {
    table.append("<tr><td>" + elem.id + "</td>" + "<td>" + elem.user + 
    "</td>" + "<td>" + elem.email + "</td>" + "<td>" + elem.summary + "</td>" +
    "<td>" + elem.due_date + "</td>" + "<td>" + elem.problem_type + "</td>"+
    "<td>" + elem.status + "</td></tr>");
});

选项 2

<thead>另一种选择是用和重写你的表<tbody>

HTML

<table id="list">
    <thead>
        <tr>
            <th>ID</th>
            <th>User</th>
            <th>Email</th>
            <th>Summary</th>
            <th>Due Date</th>
            <th>Problem Type</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <!-- Throw stuff in here -->
    </tbody>
</table>

JS

var table = $("#list");

var tr = '';

$.each(data, function(idx, elem) {
    tr += "<tr><td>" + elem.id + "</td>" + "<td>" + elem.user + 
    "</td>" + "<td>" + elem.email + "</td>" + "<td>" + elem.summary + "</td>" +
    "<td>" + elem.due_date + "</td>" + "<td>" + elem.problem_type + "</td>"+
    "<td>" + elem.status + "</td></tr>";
});

table.find('tbody').html(tr);
于 2013-11-14T19:06:48.117 回答
0

$('#list').empty(); <---- 你这样做是为了整张桌子都是空的。

我会分开,当按钮单击时,只需清空

    $('#list > tbody').empty();
于 2013-11-14T18:56:37.763 回答