我在违规表中有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>
Ajax
function scorePlus (id)
{
var total = id + id;
document.getElementById('total').value = total;
}
答案 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>