从数据库计算数组数据的数组javascript

时间:2016-06-08 06:32:19

标签: javascript php jquery arrays

我被below links文字框自动计算。

我想用数组数据库中的事件onchange数组输入来计算。

HTML:

onchange

JS:

<td class="text-left"><input type="text" name="nilaia[]" id="nilaia" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaib[]" id="nilaib" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaic[]" id="nilaic" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaid[]" id="nilaid" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaie[]" id="nilaie" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="total[]"  id="total"  readonly="true" class="form-control"></td>

3 个答案:

答案 0 :(得分:0)

如果您可以将输入的名称更改为相同名称,则可以尝试以下代码:

HTML:

<td class="text-left"><input type="text" name="nilaia[]" id="nilaia" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaia[]" id="nilaib" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaia[]" id="nilaic" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaia[]" id="nilaid" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilaia[]" id="nilaie" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="total[]"  id="total"  readonly="true" class="form-control"></td>

JS:

function hitungtotal(){
    var inputs = document.getElementsByName('nilaia[]');
    var sum = 0;
    for(var i = 0; i<inputs.length; i++){
      sum += parseInt(inputs[i].value);
    }
    document.getElementById('total').value = sum;

  }

小提琴:https://jsfiddle.net/xkuyxbof/

答案 1 :(得分:0)

尝试这个jquery回答

$('input[id^="nilai"]').change(function(){
         var total = 0;
         $('input[id^="nilai"]').each(function(){
            total+=parseFloat($(this).val());
         });
         $('#total').val(total);
});

https://jsfiddle.net/2d4cvrwz/

答案 2 :(得分:0)

试试这个

<p>A function is triggered when the user is pressing a key in the input field.</p>
<td class="text-left"><input type="text" name="nilai" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilai" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilai" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilai" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" name="nilai" onchange="hitungtotal();" value="0" class="form-control"></td>
<td class="text-left"><input type="text" id="total" readonly="true" class="form-control"></td>

<script type="text/javascript">
function hitungtotal() {
    var elems = document.getElementsByName('nilai');
    var sum = 0;     
    for(var i =0;i<elems.length;i++){
    sum+=parseInt(elems[i].value)
    }
document.getElementById('total').value  = sum;

}
</script>