如何解决如何使用jQuery从输入框值中获取总和?
我对javascript不太熟悉,所以我认为我需要你们的一些帮助。我想在输入框上显示小计的总和。
我想使用jQuery对html表行中的所有文本框值求和。这是表格:
我需要动态计算所有小计的总计并显示在输入框中。
<html class="h-100" dir="ltr">
<head>
<title>Admin</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body class="h-100">
<div class="container-fluid h-100">
<div class="row h-100">
<div class="col-md-2 h-100 bg-success" id="sidebar"></div>
<div class="col-md-10">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Description</th>
<th>Select</th>
<th>Number</th>
<th>Price</th>
<th>Sum</th>
</tr>
</thead>
<tbody>
@foreach($service->invoices as $invoice)
<tr>
<td>{{ $invoice->description }}</td>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="custom_{{ $invoice->id }}">
<label class="custom-control-label" for="custom_{{ $invoice->id }}"></label>
</div>
</td>
<td>
<div class="form-row justify-content-center">
<div class="form-group">
<div class="input-group mx-auto mb-3">
<div class="input-group-prepend">
<button class="btn btn-primary" onclick="incrementValue({{$invoice->id}})" type="button">+</button>
</div>
<input class="form-control" id="num{{$invoice->id}}" type="text" placeholder="">
<div class="input-group-append">
<button class="btn btn-primary" onclick="incrementValue({{$invoice->id}})" type="button">-</button>
</div>
</div>
</div>
</div>
</td>
<td>{{ $invoice->price }}</td>
<td><span id="totalPrice">0</span></td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="text-left">Total:
<span id="total" class="translate">0</span>
</div>
</div>
</div>
</div>
@section('script')
<script>
function incrementValue(id)
{
let value = parseInt(document.getElementById('num'+id).value,10);
value = isNaN(value) ? 0 : value;
value += 1;
document.getElementById('num'+id).value = value;
let values = parseInt(document.getElementById('sum').innerHTML,10);
// values = isNaN(values) ? 1 : (values + {{ $invoice->price }});
values = isNaN(values) ? 1 : (values + {{ $invoice->price }});
document.getElementById('totalPrice').innerHTML = values;
}
</script>
@endsection
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。