简体   繁体   English

基于表单输入字段值的动态字段生成

[英]dynamic fields generation based on form input field value

In html form i have a field as number of subjects.在 html 表单中,我有一个字段作为主题数。 My question is if i given the value as 4, then four text fields needs to be created.我的问题是,如果我将值设为 4,则需要创建四个文本字段。 With each row taking the input field value for subject name and marks.每行都采用主题名称和标记的输入字段值。 Please help me how to write java code to generate the fields dynamically based on input value.请帮助我如何编写java代码以根据输入值动态生成字段。

You mentioned Java but tagged the question JavaScript.您提到了 Java,但标记了问题 JavaScript。 So I am assuming you mean JavaScript.所以我假设你的意思是 JavaScript。

That said, you can manipulate HTML in accordance with the DOM( https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction ) and use the .appendChild( https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild ) method to insert the new text fields.也就是说,您可以根据 DOM ( https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction ) 操作 HTML 并使用 .appendChild( https://developer.mozilla .org/en-US/docs/Web/API/Node/appendChild ) 方法插入新的文本字段。

HTML: HTML:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>

        <h1>Enter number of subjects:</h1>

        <input type="number" id="inputValue">

        <button type="button" id="submitInput">Submit</button>

        <br>
        <br>

        <form id="form">


        </form>

        <script src="test.js"></script>

    </body>
</html>

JavaScript: JavaScript:

const numberOfInputs = document.getElementById('submitInput');

numberOfInputs.onclick = function() {

    const inputValue = document.getElementById("inputValue").value;

    for (let i = 0; i < inputValue; i++) {

        document.getElementById("form").appendChild(document.createElement("input"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM