質問を入力しようとしていますが、この質問をフォーム内の要素として表示するには、"survey-form"
。しかし、JavaScriptを使用して子ノードを"survey-form"
に追加すると、テキストは0.5秒間表示されてから消えます。それは、ラベル要素が追加されたが削除されたようだ。どのようにこれを修正するための任意のアイデア?appendChild関数がフォームにラベルを追加していません
window.onload = init;
function init() {
document.getElementById("question-form").addEventListener("submit",
validateQuestion);
}
function validateQuestion() {
let questionValue = document.getElementById("question-box").value;
if (questionValue == "" || questionValue == undefined) {
alert("Fill the box with information so it can be added to the survey!");
return;
} else {
addQuestion(questionValue);
}
}
function addQuestion(question) {
let label = document.createElement("label");
label.innerHTML = question;
document.getElementById("survey-form").appendChild(label);
}
<!DOCTYPE html>
<html>
<head>
<title>Create Your Own Survey</title>
<link rel="stylesheet" type="text/css" href="index.css">
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div id="model-side">
<form id="question-form" method="" action="">
<label>Create a question for the survey:</label>
<input id="question-box" type="text" size="35" /><br/>
<input type="submit" value="Submit Question" />
</form>
</div>
<div id="view-side">
<form id="survey-form" method="" action="">
<label>Current View</label>
</form>
</div>
</body>
</html>
利用event.preventDefault(); validateQuestion関数にあります。 – Lalit