Ajax中的总和不适用于laravel5.4

时间:2018-10-26 21:02:05

标签: ajax laravel

我在违规表中有score字段。而且我有很多复选框。当我选中复选框时,总分数是用ajax收集的。

<div class="panel-body">
    <div class="table-responsive">
        <table class="table table-bordered">
            @foreach($infractions as $infraction)
                <tr>
                    <th>{{ $infraction->title }}</th>
                    <td>
                        <input type="checkbox" data-toggle="toggle" data-on="Yes" data-off="No" data-onstyle="success" data-offstyle="danger" data-score="{{ $infraction->score }}" onClick="updateTotal();">
                    </td>
                </tr>
            @endforeach
        </table>
    </div>
    <div class="text-right">Total:
        <span id="total">

        </span>
    </div>
</div>

Score

Ajax

function scorePlus (id)
{
    var total = id + id;
    document.getElementById('total').value = total;
}

1 个答案:

答案 0 :(得分:1)

这应该有效

<div class="panel-body">
    <div class="table-responsive">
        <table class="table table-bordered">
            @foreach($infractions as $infraction)
                <tr>
                    <th>{{ $infraction->title }}</th>
                    <td>
                        <input type="checkbox" data-score="{{ $infraction->score }})" onClick="updateTotal();">
                    </td>
                </tr>
            @endforeach
        </table>
    </div>
    <div class="text-right">Total:
        <span id="total">0</span>
    </div>
</div>

<script>
    function updateTotal() {
        var checked = document.querySelectorAll('input[type=checkbox]:checked');
        var score = 0;

        Array.prototype.forEach.call(checked, function(el, i){
            score += parseInt(el.getAttribute('data-score'));
        });

        document.getElementById('total').innerHTML = score;
    }
</script>