首页 文章

总结输入值

提问于
浏览
2

我正在尝试做的是获得10个文本框的所有值,并获得总数并在提交时打印它 . 我做了 var id = getelementbyid(id) 然后 x=var1+var2 但没有任何反应 . 请帮帮我 .

<script type="text/javascript">
  function test(){ 
  document.getElementById('out').style.display = "block";
  return false; 
  } 
  </script>
<form action="" onsubmit="return test()"> 
  <div class="selftest"> 
  <p><input type="text" size="1" id="text1" /><label>  I Love Animals</label></p> 
  <p><input type="text" size="1" id="text2" /><label>  I Pay Close Attention To Details</label></p> 
  <p><input type="text" size="1" id="text3" /><label>  I Communicate Well With Others</label></p> 
  <p><input type="text" size="1" id="text4" /><label>  I Am Very Dependable</label></p> 
  <p><input type="text" size="1" id="text5" /><label>  I Get Along Well With Others</label></p> 
  <p><input type="text" size="1" id="text6" /><label>  I Am In Good Physical Shape</label></p> 
  <p><input type="text" size="1" id="text7" /><label>  I Understand I May Be Bitten</label></p> 
  <p><input type="text" size="1" id="text8" /><label>  I Handle Stress Well</label></p> 
  <p><input type="text" size="1" id="text9" /><label> I Am Well Organized</label></p> 
  <p><input type="text" size="1" id="text10" /><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: &lt;result&gt; 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 回答

  • 1

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

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

    参考有用的功能:

  • 1

    这是我的版本 - 你可以混合搭配,但我的至少有一个改进( Span )

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

相关问题