简体   繁体   English

如何重置文本框的值

[英]how to reset the value of a textbox

I'm making just a simple math learning helper to help me learn javascript. 我只是在做一个简单的数学学习助手来帮助我学习JavaScript。 I want to refresh The textbox after a wrong answer(the page refreshes after a right answer to get a new question), but I want the question to be the same if you get it wrong. 我想刷新错误答案后的文本框(页面得到正确答案后刷新以获取新问题),但是如果您输入错误,我希望问题也相同。 Here's my code: 这是我的代码:

<!doctype html>
  <body>
    <center>
      <font size="5">
        <form Id="Input">
          <input type="text" name="Input">
          <input type="button" value="submit" ID="Button">
        </form>

    <script type="text/javascript">
      Button.addEventListener("click", Answer);
      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() {
        if(Input.Input.value == C) {
          alert("correct!");
          window.location.reload();
        } else {
          alert("incorrect!");
          document.getElementById('txtField').value='new value here'
        }
      }
    </script>
  </body>
</html>

您只需要将txtField id赋予文本输入即可,这里是一个有效的小提琴http://jsfiddle.net/eFf27/

<input id="txtField" type="text" name="Input">

You forgot to add an ID to your text box. 您忘记在文本框中添加ID。

<!doctype html>
<body>
<center>
<font size="5">
<form Id="Input">
<input id="txtField" type="text" name="Input">
<input type="button" value="submit" ID="Button">
</form>
<script type="text/javascript">
Button.addEventListener("click", Answer);
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()
{
if(Input.Input.value == C)
{
alert("correct!");
window.location.reload();
}
else
{
alert("incorrect!");
document.getElementById('txtField').value='new value here'
}
}
</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM