首页 > 解决方案 > 用 JS 做一个简单回答的问题表格

问题描述

如何在js中制作简单答案的问题形式?我试过

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8"/>
    <script>
      function cipher1(){
        var val ="theanswer";
        if(val == (document.getElementById("cipher1").value)){
          alert("correct");
        }
      }
    </script>
  </head>
  <body id="bod">
    <div id="cipher1"style="display:none;">
      <input id="cipher1"type="text" value="write the answer here"/>
      <input type="button" class="but"value="answercheck"onclick="cipher1()"/>
    </div>
    </body>
    </html>

但它不起作用。请帮忙!!!

标签: javascriptphphtml

解决方案


您的 ID 必须是唯一的。在您的代码中,您将 'cipher1' 与 div 和输入字段相关联。您可以将文本字段的 Id 更改为 cipher2。请看下面的代码:

  function cipher1(){
        var val ="theanswer";
        if(val == (document.getElementById("cipher2").value)){
          alert("correct");
        }
      }
 <div id="cipher1">
      <input id="cipher2"type="text" value="write the answer here"/>
      <input type="button" class="but"value="answercheck"onclick="cipher1()"/>
    </div>


推荐阅读