<!DOCTYPE html>
<html>
<head></head>
<body>
<form method="POST" action="act.php">
<p>Click the button to create a DIV element with some text, and append it to DIV.</p>
<div id="myDIV">
MATHS PAPER
</div>
<button onclick="myFunction()">Add Question</button>
<input type="submit" value="Create"> </input>
<script>
function myFunction() {
var para = document.createElement("DIV");
para.innerHTML = "<div style='background-color:lightgreen'>QUESTION<div><input type='text' id='q1' placeholder='enter question'></div><div></input><input type='text' placeholder='enter option1 here'></input></div><div></input><input type='text' placeholder='enter option2 here'></input></div></div>";
document.getElementById("myDIV").appendChild(para);
}
</script>
</form>
</body>
</html>
如何使此代码有效?我在removing form tag上观察到,它正在工作。但是我希望它在表单标签中,以便我可以发布这些问题和选项,并将其保存在数据库中。
参考:https://www.w3schools.com/jsref/met_document_createelement.asp