Home  >  Article  >  Backend Development  >  文本框数组求和有关问题

文本框数组求和有关问题

WBOY
WBOYOriginal
2016-06-13 13:28:171298browse

文本框数组求和问题
HI 各位大大
  我这里碰到这样一个问题,我在PHP中了个 文本框数组
  for($i=1;$i   {
 echo "";
}

  我该如何做可以使得我在每个文本框中输入的内容进行合计(求和),并显示出来?

------解决方案--------------------

HTML code



<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function() {
    var obj = document.getElementsByName('abc[]');
    var obj_count = document.getElementById('count');
    for (var i = 0; i < obj.length; i ++) {
        obj[i].onkeyup = function() {
            var count = 0;
            for (var j = 0; j < obj.length; j ++) {
                var v = parseInt(obj[j].value) || 0;
                count += v;
            }
            obj_count.innerHTML = count;
        }
    }
}
</script>



<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<input type="text" name="abc[]" value="">
<br>
<div id="count"></div>


<br><font color="#e78608">------解决方案--------------------</font><br>用parseFloat()代替parseInt()
<br><font color="#e78608">------解决方案--------------------</font><br>额,可以js算好再ajax交上去,也可以做个表单直接交上去,也可以ajax直接交上去。 <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn