사용자가 입력 한 숫자의 합계를 자동으로 계산하고 싶은데, 사용자가 하나의 숫자를 삭제하면 자동으로 합계가 계산됩니다. jquery를 사용하여 배열 값의 합계를 계산하는 방법이 문제를 해결하도록 도와주세요.
$(document).ready(function(){
$('.form-control').change(function(){
var total = 0;
$('.form-control').each(function(){
if($(this).val() != '')
{
totalvalue += parseInt($(this).val());
}
});
$('#totalvalue').html(totalvalue);
});
})(jQuery);
<h2 style="text-align:center;">INVOICE</h2>
<form id='students' method='post' name='students' action='index.php'>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
<th>S. No</th>
<th>Job ID</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Price</th>
</tr>
<tr>
<td><input type='checkbox' class='case'/></td>
<td><span id='snum'>1.</span></td>
<td><input class="form-control" type='text' id='txt_jobid' name='txt_jobid[]'/></td>
<td><input class="form-control" type='text' id='txt_quantity' name='txt_quantity[]'/></td>
<td><input class="form-control" type='text' id='txt_price' name='txt_price[]'/></td>
<td><input class="form-control" type='text' id='txt_totalprice' name='txt_totalprice[]'/> </td>
</tr>
</table>
<button type="button" class='btn btn-danger delete'>- Delete</button>
<button type="button" class='btn btn-success addmore'>+ Add More</button>
</div>
</form> Total: <div id="totalvalue">0</div>
양식 제출
</form>
때로는 사용 total
하고 때로는 totalValue
. 변수 이름을 동일하게 지정하십시오. 그리고 (jQuery)
끝에 제거하십시오 . 거기가 잘못되었습니다.
$(function() {
$('.form-control').change(function() {
var total = 0;
$('.form-control').each(function() {
if( $(this).val() != '' )
total += parseInt($(this).val());
});
$('#totalvalue').html(total);
});
// trigger initial calculation if wanted
.change();
});
예 : https://jsfiddle.net/0zpkow5L/
초기 계산 : https://jsfiddle.net/0zpkow5L/1/
코드의 전체 작동 예 : https://jsfiddle.net/0zpkow5L/8/
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다