javascript - 总结输入值

标签 javascript

我正在尝试做的是获取 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>

如果你不明白我的问题,请提问 :)

最佳答案

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

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

关于javascript - 总结输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3954085/

相关文章:

javascript - 为什么数组推送在我的函数中不起作用?

javascript - nodejs全局变量是否绑定(bind)到 "module"函数,如何赋值?

javascript - 如何创建登录和注册页面而无需连接到后端(ReactJS + Redux)?

javascript - 无法设置 null 的属性 InnerHTML

javascript - Meteor 方法不将 html 返回到模板

javascript - 初学者 JQuery 切换类

javascript - 我可以在脚本中完全禁用 'debugger' 吗?

javascript - Promise.all 与 Firebase DataSnapshot.forEach

javascript - 如何使 div 对 Angular 线动画?

javascript - jQuery 提交即时表单同时挂起 Mozilla 和 Chrome