无法使用按钮添加DIV元素

问题描述 投票:0回答:1
<!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

javascript html django dom
1个回答
0
投票
在关闭表单标签后但在正文标签内将其写入

0
投票
您应指定按钮类型,否则默认情况下将其作为type =“ submit”。因此,将其指定为type =“ button”(noraml按钮),该表单将不会像以前一样提交。

0
投票
您的代码工作正常,但是当您单击按钮时,表单会在目标页面上发送数据
© www.soinside.com 2019 - 2024. All rights reserved.