2015-01-09 130 views
1

我創建了一個下拉表,在添加新行時從下拉列表中選擇某些內容時應刪除現有行。動態添加和刪除HTML表中的行

我可以添加,但我無法弄清楚如何刪除舊的。幫幫我!

var currencies; 

$.ajax({ 
    url: 'data.json', 
    type: "post", 
    dataType: "json", 
    method: "GET", 
    success: function (data, textStatus, jqXHR) { 
     currencies = data[0].currencies; 
     drawTable(currencies); 
    } 
}); 


function drawTable(currencies) { 
    for (var i = 0; i < currencies.length; i++) { 
     drawRow(currencies[i]); 
    } 
} 


function IfTheyPicked5Days() { 
    if ($("#dropdown")[0].value=="fivedays") { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

function redrawTableForPeopleWhoPicked1Month() { 
    drawTable(currencies); 
} 

function drawRow(rowData) { 
var row = $("<tr />") 
$("#currencyhomework").append(row); 
row.append($("<td>" + rowData.currency + "</td>")); 
row.append($("<td>" + rowData.country + "</td>")); 
row.append($("<td>" + rowData.ranges.oneday + "</td>")); 

if (IfTheyPicked5Days()){ 
    row.append($("<td>" + rowData.ranges.fivedays + "</td>")); 
} else { 
    row.append($("<td>" + rowData.ranges.onemonth + "</td>")); 
} 

}

+0

http://api.jquery.com/empty/ – 2015-01-09 18:40:21

+0

http://api.jquery.com/remove/ – Buck3y3 2015-01-09 18:40:39

回答

1
 function drawTable(currencies) { 
      //empty the table here: 
      $("#currencyhomework").empty(); 
      for (var i = 0; i < currencies.length; i++) { 
       drawRow(currencies[i]); 
      } 
     } 

每次當drawTable是叫你應清空時間。這樣,表格的每個(重新)繪製都以更新的數據開始。 Empty()jQuery的相當於element.innerHTML = null