简体   繁体   English

在数组中存储具有不同ID的多个值-PHP,JavaScript

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

Multiple values are inputted using below code: $c_num is the count of inputs. 使用以下代码输入多个值: $c_num是输入的计数。 There are n number of inputs $stud['stud_c'] is student codes, S01,S02,S05,S08,S19 ......... 输入的数量为n个$stud['stud_c']是学生代码,S01,S02,S05,S08,S19 .........

On a javascript function calculate() , a portion of id is passed. 在javascript函数calculate() ,传递了一部分id。

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. 当我提醒class1时,我正在获取相应的输出。 But how to store multiple values with different id in array? 但是如何在数组中存储具有不同ID的多个值? How to store those inputted values in array num[] ? 如何将这些输入值存储在数组num []中? I also want to alert the total of these inputs. 我还想提醒所有这些输入。

Corresponding script for finding sum of n number of inputs: 用于查找n个输入之和的相应脚本:

 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[] . array.push() 。并且数组声明是错误的。使用num=[]而不是num[]

I don't know why use n on forloop length. 我不知道为什么在forloop长度上使用n if you originaly need the array length use with n=num.length 如果您最初需要数组长度,请使用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. 注意,如果只想计算值的总和,则不需要将输入值收集到num数组中。 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: 如果出于某些原因您仍想将值收集到数组中,请使用Array.prototype.push方法:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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