如何使HTML表单启动功能 [英] How to make an html form start a function

查看:72
本文介绍了如何使HTML表单启动功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以使带有文本输入的表单按钮启动一个表单,该表单将检查答案是否正确,然后继续告诉您.这就是我所拥有的,你能告诉我我需要什么吗?

I'm wondering if there is an easy way to make a form button with text input start a form that will check if the answer's correct and then proceed to tell you. Here's what I have, can you tell me what I need?

<!doctype html>
<body>
 <center>
  <form name="Input" action="Answer" method="get">
   <input type="text" name="Input">
   <input type="submit" value="submit">
  </form>
  <script type="text/javascript">
   var A = Math.floor(Math.random()*11);
   var B = Math.floor(Math.random()*11);
   var C = A+B;
   var Input = document.getElementById('Input');
   document.write(A + "+" + B)
   function Answer()
   {
     alert("correct!!");
   }
 </script>

</body>
</html>

推荐答案

function calculateAnswer()
{
var A = Math.floor(Math.random()*11);
var B = Math.floor(Math.random()*11);
var C = A+B;
var Input = document.getElementById('Input');
if(Input.value == C)
{
document.body.innerHTML += (A + "+" + B);
//  Or another thing to do if the answer's correct
}
}

此外,将事件侦听器附加到提交"按钮以使其运行该功能:

Also, attach an event listener to the submit button to make it run the function:

referenceToTheSubmitButton.addEventListener("click", calculateAnswer);

根据需要缩进. 享受! :D

Indent as needed. Enjoy! :D

这篇关于如何使HTML表单启动功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆