javascript - 用 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>
但它不起作用。请帮忙!!!
解决方案
您的 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>
推荐阅读
- python - 在 Python 中使用 Tkinter 将按钮对齐到框架中心的最佳方法是什么
- java - 让 RecyclerView 适配器为不同的用户显示不同的视图
- spring - MongoRepository 是否使用 MongoDB 游标?
- python - 如何在 object_list 中显示外键值的总和
- ejb-3.0 - 当内部有许多 web 模块时无法部署 ear 包
- vue.js - vue 路由器是预先加载每个视图然后显示它还是通过仅在请求时加载视图服务器来实现?
- python-3.x - TemplateSyntaxError:变量“user.profile.photo”是无效来源
- php - Laravel 存储碳异常
- javascript - 反应复制查询数据导致空数组
- c++ - 将一些内容从应用程序移动到库会减少内存占用c ++