简体   繁体   中英

Storing multiple values with different id in array - php,javascript

Multiple values are inputted using below code: $c_num is the count of inputs. There are n number of inputs $stud['stud_c'] is student codes, S01,S02,S05,S08,S19 .........

On a javascript function calculate() , a portion of id is passed.

code:

<input type="number" required id="c_m_<?php echo $c_num; ?>_<?php echo $stud['stud_c'];?>" name="c_m_<?php echo $c_num; ?>[<?php echo $stud['stud_c'];?>]" onkeypress="calculate('<?php echo $c_num; ?>_<?php echo $stud['stud_c'];?>','<?php echo $stud['stud_c'];?>')" class="num<?php echo $c_num; ?> " >

When I alert class1, i am getting corresponding output. But how to store multiple values with different id in array? How to store those inputted values in array num[] ? I also want to alert the total of these inputs.

Corresponding script for finding sum of n number of inputs:

 function calculate(class1,class2){     
    var n = parseFloat(document.getElementById('count').value);
    var num[] = parseFloat(document.getElementById('c_m_'+class1).value);
    var total=0;
    for (var i = 0; i <n; i++) {

       total = total + parseFloat(num[i]) ;
    }
     if (!isNaN(total)) {
       document.getElementById('total_mark_'+class2).value = Math.round(total);

    }
 }

Try like this..

<script>
arr =[];
function calculate(class1)
{       
//var n = parseFloat(document.getElementById('count').value);
var num = parseFloat(document.getElementById('c_m_'+class1).value);
arr.push(num);
var total=0;
var n =arr.length;
for (var i = 0; i <n; i++) {

       total = total +arr[i];
}
alert(total);
}
</script>

Try this . array.push() .And array declaration was wrong.use num=[] instead of num[] .

I don't know why use n on forloop length. if you originaly need the array length use with n=num.length

  var num=[]; function calculate(class1) { var n = parseFloat(document.getElementById('count').value); num.push(parseFloat(document.getElementById('c_m_'+class1).value)); var total=0; //n=num.length if you need num array length use this for (var i = 0; i <n; i++) { total = total + parseFloat(num[i]) ; } alert(total); } 

Assign a common attribute for the inputs and fetch them by this attribute. There are many ways to fetch nodes by attribute. To name a few:

Example

 (function() { var inputs, i, total = 0; inputs = document.forms.c.querySelectorAll("input[name='c_m[]']"); for (i = 0; i < inputs.length; i++) { total += Number(inputs[i].value); } console.log(total); })(); 
 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>XXX</title> </head> <body> <form name="c"> <input type="text" name="c_m[]" value="1" size="3" /> <input type="text" name="c_m[]" value="2" size="3" /> <input type="text" name="c_m[]" value="3" size="3" /> </form> </body> </html> 

Note, you don't need to collect the input values into num array, if you only want to compute the sum of the values. Simply literate the nodes and collect their values. If for some reason you still want to collect the values into an array, use Array.prototype.push method:

var num = [];
for (i = 0; i < inputs.length; i++) {
  num.push(inputs[i].value);
}
<script type="text/javascript">
    var RECALCULATE = function(a,b) { 
    var rowtotal = 0;
    n = new Array();
    var count = document.getElementById('criteria_count').value;    

    for (i = 1; i <=count; i++) { 
      if (parseInt(document.getElementById('criteria_mark_'+i+'_'+b).value) > 0) {

           n[i] = parseInt(document.getElementById('criteria_mark_'+i+'_'+b).value);
           rowtotal += n[i];
       }
    }

     document.getElementById('total_mark_'+b).value = rowtotal;
};
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM