AJAX를 사용하여 데이터베이스의 항목에서 테이블에 행을 추가하는 코드가 있습니다. 그 부분은 잘 작동하지만 내가 만든 행의 숫자에서 합계를 계산해야하지만 어떤 이유로 시도한 모든 것이 작동하지 않았습니다.
내 코드 :
$.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>
이것은 데이터를 테이블에 성공적으로 삽입하지만 합계를 얻으려고 Duration
하지만 작동하지 않는 것 같습니다. 팁이 있습니까?
테이블을 만들 때 각 "duration"클래스를 부여한 다음 각 요소를 반복하고 값을 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이 반환됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다