单击按钮时添加新表单

我正在尝试创建一个需要用户多次输入的计算器,次数由用户决定。我想在单击按钮时出现一个新表单。


我试图在按钮内创建 onclick 事件,但是当点击它时什么也没有发生。


请参阅下面的相关代码


 

let addButton = function() {

    let mile2Form = document.createElement('input');

    mile2Form.setAttribute('type', 'time');

    document.body.appendChild(mile2Form);

}

 

<section>

    <button class= "addButton" onclick="addButton()">Add Another Time</button>

</section>

我希望创建一个新表单。


任何帮助将非常感激!


开心每一天1111
浏览 347回答 1
1回答

繁星coding

您可以创建createElement("form");控件并将其添加到表单中form.appendChild(mile2Form);也是一个很好的约定,您应该在</body>标记之前移动脚本块let addButton = function() {var form = document.createElement("form");form.setAttribute('method',"post");form.setAttribute('action',"#");&nbsp; &nbsp; let mile2Form = document.createElement('input');&nbsp; &nbsp; mile2Form.setAttribute('type', 'time');&nbsp; &nbsp; form.appendChild(mile2Form);&nbsp; &nbsp; document.body.appendChild(form);}let addButton = function() {var form = document.createElement("form");form.setAttribute('method',"post");form.setAttribute('action',"#");&nbsp; &nbsp; let mile2Form = document.createElement('input');&nbsp; &nbsp; mile2Form.setAttribute('type', 'time');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; form.appendChild(mile2Form);&nbsp; &nbsp; document.body.appendChild(form);}<section><button class= "addButton" onclick="addButton()">Add Another Time</button></section>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript