【问题标题】:sum up values of inputs总结输入值
【发布时间】:2011-04-26 15:04:01
【问题描述】:

我想要做的是获取 10 个文本框的所有值,然后在提交时打印出来。我做了 var id = getelementbyid(id) 然后 x=var1+var2 但没有任何反应。请帮我解决这个问题。

如果您不理解我的问题,请提出问题 :)

标签: javascript


【解决方案1】:

这是我的版本——你可以混搭,但我的版本至少有一个改进(跨度)

<script type="text/javascript">
var nofQuestions = 10; // or scan the complete form as already posted
function test(theForm) {
  var total=0;
  for (var i=1;i<=nofQuestions;i++) { // or use document.getElementById and retain the IDs
    var val = theForm.elements["text"+i].value; 

    total+= (isNaN(val) || !val)?0:parseInt(val,10);
  }
  document.getElementById('result').innerHTML=total;
  document.getElementById('out').style.display = "block";
  return false; 
} 
</script>
<form action="" onsubmit="return test(this)"> 
  <div class="selftest"> 
  <p><input type="text" size="1" name="text1"  value="" /><label>  I Love Animals</label></p> 
  <p><input type="text" size="1" name="text2"  value="" /><label>  I Pay Close Attention To Details</label></p> 
  <p><input type="text" size="1" name="text3"  value="" /><label>  I Communicate Well With Others</label></p> 
  <p><input type="text" size="1" name="text4"  value="" /><label>  I Am Very Dependable</label></p> 
  <p><input type="text" size="1" name="text5"  value="" /><label>  I Get Along Well With Others</label></p> 
  <p><input type="text" size="1" name="text6"  value="" /><label>  I Am In Good Physical Shape</label></p> 
  <p><input type="text" size="1" name="text7"  value="" /><label>  I Understand I May Be Bitten</label></p> 
  <p><input type="text" size="1" name="text8"  value="" /><label>  I Handle Stress Well</label></p> 
  <p><input type="text" size="1" name="text9"  value="" /><label> I Am Well Organized</label></p> 
  <p><input type="text" size="1" name="text10" value="" /><label> I Am Intelligent</label></p> 
  <p><input type="submit" value="Submit Answers" /></p> 
  </div> 
  </form> 
<div id="out" style="display:none;"> 
  Here are the results of your Veterinary Technician Self Test: <span id="result"></span> Obviously veterinary technicians must love animals, but they must love to be around and communicate with people also because they spend much of their time interacting with co-workers and pet owners. An individual should be able to take a leadership role and be able to talk with strangers, to be a good candidate for a veterinary technician career. Those who want to become veterinary technicians must have good communication skills and have the ability to work well with others. They must also be well-organized and be able to pay attention to detail. They must be of above average intelligence and have a good understanding of math and science. It is necessary that a veterinary technician be dependable since an animal’s life may rest in their hands when treatments must be delivered in a timely manner. A veterinary technician’s job can be very demanding. He or she must be able to spend hours on their feet and have the ability to lift and restrain animals. Unfortunately, being bitten is part of the job. It does not occur often, but a veterinary technician must realize that it is a real possibility. Due to the possibility of emergency situations, a veterinary technician must be able to handle stress well. From the results of the self test, you have many of the characteristics needed to work in this field, but you may need to do some more research before you go any further. Start by reading <a href="http://www.vettechuniversity.com/veterinary-technician-career-information/" target="_blank"><strong>Veterinary Technician Career Information</strong></a>. 
  </div>

【讨论】:

    【解决方案2】:

    如果你想计算所有值的总和,你需要先将它们转换为整数:

    var total = 0;
    for (var i = 1; i <= 10; i++) {
        // get the DOM element by id
        var element = document.getElementById('text' + i);
        if (element != null) {
            // if the element is found try parsing it's value to integer
            var value = parseInt(element.value);
            if (!isNaN(value)) {
                // if the parsing was successful add to the total amount
                total = total + value;
            }
        }
    }
    alert(total);
    

    有用功能参考:

    【讨论】:

    • 虽然如果用户输入的任何数字大于 7,我会使用 parseInt(element.value, 10)。否则,当用户键入“08”或“010”并且计算机将其解释为 八进制 而不是十进制时,您会遇到问题。
    猜你喜欢
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    相关资源
    最近更新 更多