繁体   English   中英

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

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

使用以下代码输入多个值: $c_num是输入的计数。 输入的数量为n个$stud['stud_c']是学生代码,S01,S02,S05,S08,S19 .........

在javascript函数calculate() ,传递了一部分id。

码:

<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; ?> " >

当我提醒class1时,我正在获取相应的输出。 但是如何在数组中存储具有不同ID的多个值? 如何将这些输入值存储在数组num []中? 我还想提醒所有这些输入。

用于查找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);

    }
 }

像这样尝试

<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>

尝试这个 。 array.push() 。并且数组声明是错误的。使用num=[]而不是num[]

我不知道为什么在forloop长度上使用n 如果您最初需要数组长度,请使用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); } 

为输入分配一个公共属性,并通过该属性获取它们。 通过属性获取节点的方法有很多。 仅举几例:

 (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> 

注意,如果只想计算值的总和,则不需要将输入值收集到num数组中。 只需对节点进行扫盲并收集其值即可。 如果出于某些原因您仍想将值收集到数组中,请使用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