javascript - 为什么我的 HTML 按钮没有注册点击?
问题描述
我正在提交表单并根据所选响应显示内容。现在我只是想让按钮工作,这样它就可以从一个问题转到显示正确答案的显示屏,然后再转到下一个问题。index.js
function startQuiz() {
$('#startButton').click(function(event){
console.log("Keep Going");
renderQuestion();
}
);
}
let questionNumber= 0
function renderQuestion() {
console.log("hello");
let questions = STORE.questions[questionNumber];
const questionTemplate = $( `
<div class="container" align="center">
${questions.image}
<p>${questions.questionText}<br></p>
<form method="post" onsubmit="return nextQuestion()">
<input type="radio" name="choice">${questions.responses[0]}<br>
<input type="radio" name="choice">${questions.responses[1]}<br>
<input type="radio" name="choice">${questions.responses[2]}<br>
<input type="radio" name="choice">${questions.responses[3]}<br>
<button input type="submit" id="choose" value="submit">Submit</button>
</form>
</div>`);
$("main").html(questionTemplate);
}
function checkAnswer() {
$("main").on("click","#choose",function(event){
let questions = STORE.questions[questionNumber];
event.preventDefault();
console.log("clicked");
const correctResponse = $(`
<div class="container" align="center">
${questions.image}
<h4>${questions.answer}</h4>
<p>Correct!<p>
<button input type="submit id="next" value="submit">Next</button>
</div>`);
$("main").html(correctResponse);
})
}
function nextQuestion(){
$("main").on("click","#next",function(event){
event.preventDefault();
console.log("Don't turn back");
questionNumber= questionNumber +1;
renderQuestion();
})}
$(checkAnswer)
$(startQuiz)
我创建了一个console.log()
,所以我可以看到何时按下按钮但它没有出现。另一方面,它并没有刷新页面,它似乎没有做任何事情。
索引.html
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Herbal Medicine: Adaptogens</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="questions" href="store.html">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="index.js"></script>
<script src="store.js"></script>
</head>
<body>
<h1>Herbal Medicine: Adaptogens</h1>
<main>
<div class="container" class="firstLetter">
<p>Adaptogens are a category of healing herbs that have a number of beneficial qualities for the human body. They
are characterized by their ability to perform well with other medications and supplements, they can build up in
the system without negative effects, and they help the body adapt to stress on many levels. <br>
Stress can be emotional, physical and mental and the various adaptogens tend to apply their healing qualities to those areas
of the body that most need support.<br>
Herbal medicine has been cataloged throughout the ages, with physical records uncovered as early as 1500 BCE.
Most prescription medications have been derived from plants, and their latin or botanical names will indicate
such.<br>
Test your knowledge and learn a thing or two about healing herbs!<br>
</p>
<button id="startButton">Start</button>
<br> <h12>The statements in this quiz are not intended to diagnose or treat any conditions. When in doubt ask a pro!</h12>
</div>
</main>
</body>
</html>
store.js
const STORE = {
questions: [ {
image: `<div class="image"> <img src="nettles.svg", alt="mystery plant illustration"> </div>`,
questionText: "Which plant provides the most minerals and nutrients.",
responses: [
"Licorice <i>Glycyrrhiza glabra</i>",
"Jiagulon <i>Gynostemma pentaphyllum</i>",
"Lemon Balm <i>Melissa Officianlis</i>",
"Nettles <i>Urtica dioica</i>"
],
answer: "Nettles <i>Urtica dioica</i>"
},
{
questionText: "Which Plant is known for lowering high cholesterol, high blood pressure, and improving heart function as well as improving memory, and preventing hair loss?",
responses: [
"Jiagulon <i>Gynostemma pentaphyllum</i>",
"Licorice <i>Glycyrrhiza glabra</i>",
"Nettles <i>Urtica dioica</i>",
"Lemon Balm <i>Melissa Officianlis</i>"
],
answer: "Jiagulon <i>Gynostemma pentaphyllum</i>"
}
]
}
解决方案
在 index.js 中试试这个,
function checkAnswer() {
$("main").on("click","#choose",function(event){
let questions = STORE.questions[questionNumber];
event.preventDefault();
console.log("clicked");
const correctResponse = $(`
<div class="container" align="center">
${questions.image}
<h4>${questions.answer}</h4>
<p>Correct!<p>
<button input type="submit" id="next" value="submit">Next</button>
</div>`);
$("main").html(correctResponse);
// for registering #next id event
nextQuestion();
})
}
//说明:
函数 nextQuestion() 正在注册您的#next 点击事件,在 DOM 中,#next id 将出现在 $("main").html(correctResponse); 之后 线。所以 nextQuestion() 应该在#next id 成功放入 DOM 时调用。
推荐阅读
- python - 从图像中剪下一块不是矩形的(例如梯形),然后将其变成适合板的矩形
- css - 如何在 rtl css 和 ltr css 生成的 rtl css 文件之间进行选择
- c# - 如何修复“无效的登录令牌”。尝试将经过身份验证的用户与 AWS 和 Cognito 用户池一起使用时
- visual-studio - Visual Studio 2017 中的 ROOT cern
- javascript - Git 在发出拉取请求时会看到分支中的许多更改
- php - PHP数组:条件和
- android - 以编程方式将位置添加到推文
- vue.js - 具有隐藏/显示面板的灵活布局
- docker - 如何在 Docker 上为 Vue-router 配置 nginx
- three.js - 如何设置初始相机位置(Three.js/VR)?