UYOU
2022-09-23 21:28:25
我有一個代碼,它使用AJAX從數(shù)據(jù)庫中的項向表中添加行。該部分工作正常,但我需要計算我創(chuàng)建的行中的數(shù)字的總和,但由于某種原因,我嘗試的所有內(nèi)容都不起作用。我的代碼 :$.ajax({ type: "GET", url: "http://localhost:8080/api/001, dataType: "json", success: function(data) { for (var count = 0; count < data.length; count++) { $('<tr>').append( $('<td>').text(dateArray[0]), $('<td class="duration">').text(dateArray[1]), ).appendTo('#testtable'); }, error: function(jqXHR, textStatus, errorThrown) {} });<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table id="testtable"> <tr> <th>Date</th> <th>Duration</th> </tr>這成功地將數(shù)據(jù)插入到表中,但我試圖獲取的總和,但似乎無法使其正常工作。有什么提示嗎?Duration
1 回答

忽然笑
TA貢獻(xiàn)1806條經(jīng)驗 獲得超5個贊
創(chuàng)建表時,應(yīng)為每個“持續(xù)時間”類指定,然后循環(huán)訪問每個元素并將值存儲為 int。例如:
<table id="testtable">
<tr>
<th>Date</th>
<th>Duration</th>
</tr>
<tr class="duration"><td >5</td><td class="duration">5</td></tr>
</table>
<script>
jQuery(document).ready(function(){
// Loop through each div element with the class box
total =0;
$(".duration").each(function(){
stringval = jQuery(this).text();
num = parseInt(stringval);
total += num;
;
});
console.log(total);
});'''
</script>
這將返回10
添加回答
舉報
0/150
提交
取消