首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在J.S中制作一个简单回答的问题形式

在J.S中制作一个简单回答的问题形式
EN

Stack Overflow用户
提问于 2018-08-01 17:50:18
回答 3查看 59关注 0票数 0

如何在js中制作一个简单答案的问题表单?我试过了

代码语言:javascript
复制
<!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>

但是它不起作用。请帮帮我!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-01 18:00:20

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

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

票数 2
EN

Stack Overflow用户

发布于 2018-08-01 17:56:36

首先尝试更改两个重复的ID

代码语言:javascript
复制
div id="**cipher1**"style="display:none;">
      <input id="**cipher1**"type="text" value="write the answer here"/>

然后从父div中删除显示样式

票数 1
EN

Stack Overflow用户

发布于 2018-08-01 18:14:15

您在页面上有重复的id (您在页面上只能有一个id ) cipher1

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51630250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档