HTML(仅限)基于用户输入的表单

huangapple go评论62阅读模式
英文:

HTML (Only) FORM Based on user input

问题

如何让用户在HTML表单中添加自定义数据。我们与一些业务合作伙伴有多个地址:

示例:

  • 你有多少个地址:5
  • 然后表单将为5个不同的地址填充足够的输入框。
<form action="/action_page">
    <label for="fname">组织名称:</label><br>
    <input type="text" id="fname" name="fname" value="ABC公司"><br>
    <label for="lname">姓氏:</label><br>
    <input type="text" id="lname" name="lname" value="Doe"><br><br>
</form>

注意:上面的示例只包括两个输入框的部分,你需要根据需要重复创建足够多的输入框来满足用户输入多个地址的要求。

英文:

how do i let the user add its own custom data in html form. some businesses we deal with have multiple addresses:

Example:

  • How mane locations do you have: 5
  • the form will then populate enough input boxes for 5 different addresses.

form action="/action_page">
<label for="fname">Orgnization Name:</label><br>
<input type="text" id="fname" name="fname" value="ABC Company"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>

答案1

得分: 0

你可以运行一个循环来创建输入元素。

<body>
    <form action="" method="">
        <input type="number" id="noOfAddresses" style="margin-bottom: 10px;"> 地址数量。
        <button type="button" id="addBtn">添加</button>
        <div id="addressInputs" style="display: flex; flex-direction: column; width: 170px; gap: 10px;"></div>
    </form>

    <script>
        let addBtn = document.getElementById("addBtn");
        let addressInputs = document.getElementById("addressInputs");

        addBtn.addEventListener("click", () => {
            let noOfAddresses = parseInt(document.getElementById("noOfAddresses").value);
            for (let i = 0; i < noOfAddresses; i++) {
                const input = document.createElement("input");
                addressInputs.appendChild(input);
            }
        })
    </script>
</body>
英文:

You can run a loop that creates the Input element.

&lt;body&gt;
    &lt;form action=&quot;&quot; method=&quot;&quot;&gt;
        &lt;input type=&quot;number&quot; id=&quot;noOfAddresses&quot; style=&quot;margin-bottom: 10px;&quot;&gt; No. of Addresses.
        &lt;button type=&quot;button&quot; id=&quot;addBtn&quot;&gt;Add&lt;/button&gt;
        &lt;div id=&quot;addressInputs&quot; style=&quot;display: flex; flex-direction: column; width : 170px; gap : 10px;&quot;&gt;&lt;/div&gt;
    &lt;/form&gt;

    &lt;script&gt;
        let addBtn = document.getElementById(&quot;addBtn&quot;); 
        let addressInputs = document.getElementById(&quot;addressInputs&quot;); 
    
        addBtn.addEventListener(&quot;click&quot;, ()=&gt;{

        let noOfAddresses = parseInt(document.getElementById(&quot;noOfAddresses&quot;).value); 
        for (let i = 0; i &lt; noOfAddresses; i++){
            const input = document.createElement(&quot;input&quot;); 
            addressInputs.appendChild(input); 
        }
    })

    &lt;/script&gt;
&lt;/body&gt;

huangapple
  • 本文由 发表于 2023年1月9日 14:39:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定