如何使用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>

price

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)