简体   繁体   English

如何将动态创建的JavaScript表单字段中的表单数据接收到JSP中?

[英]How do i receive form data from the dynamically created JavaScript form fields into JSP?

however, when i tried to get the values from the dynamically created form fields, i get null values from the form submission but not with the text fields created in html. 但是,当我尝试从动态创建的表单字段中获取值时,我从表单提交中获取了空值,但没有使用html中创建的文本字段。 Any help would be appreciated. 任何帮助,将不胜感激。

small code snippet. 小代码段。

            for (i=0;i<number;i++){


            // Append a node with a random text
            container.appendChild(document.createTextNode(("Sl.No ")));
            container.appendChild(document.createTextNode((i+1)));
            // Create an <input> element, set its type and name 
             attributes

            //name
            var input = document.createElement("input");
            input.type = "text";
            input.name = "name" + i;
            container.appendChild(input);

            //age
            var input = document.createElement("input");
            input.type = "text";
            input.name = "age" + i;
            container.appendChild(input);

            //book button
        var input = document.createElement("input");
        input.type = "submit";
        input.name = "sbtn";
        input.value="Book";

        }

Below code gets dynamically created fields perfectly submitted. 下面的代码可完美地提交动态创建的字段。

HTML: HTML:

<form id="myForm" action="/" method="post">
<label id="authlabel">User Authentication method:</label>
<br>
<select id="authMethod" name="authmethod">
  <option disabled selected value> -- select an option -- </option>
  <option value="userName">Name</option>
  <option value="userId">User ID</option>
</select>
<br>
<input type="submit" value="submit"></input>
</form>

JS: JS:

const myForm = document.querySelector('#myForm');

document.querySelector('#authmethod').addEventListener('change', function(event){
    let authInput = document.createElement('input');
    authInput.name = event.target.value;
    myForm.insertBefore(authInput, event.target.nextSibling);
});

If, for some reason, it doesn't work for you, would you submit your HTML and full javascript context? 如果由于某种原因对您不起作用,您是否会提交HTML和完整的javascript上下文?

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

相关问题 如何使用从Javascript函数获得的数据填充JSP中的表单 - How do I populate a form in JSP with data obtained from a Javascript function ReactJS:如何从外部应用程序接收表单数据 - ReactJS: how do I receive form data from external application get函数不会从表单中动态创建的字段中获取数据 - get function does not fetch data from dynamically created fields in form 如何引用动态创建的表单域 - How to reference dynamically created form fields 如何使用javascript将表单值传递给表单中的其他字段 - How do I pass form values to other fields in a form with javascript 如何使用 javascript 动态复制或添加更多 html 表单字段? - How do i replicate or add more html form fields dynamically with javascript? 从Javascript对象循环动态创建的表单。 如何将数据保存回对象 - Form created dynamically from Javascript Object loop. How to save data back to object 如何以动态形式创建动态创建的下拉列表? - How do I create dynamically created dropdowns in a dynamic form? 如何从通过 Javascript function 动态创建的输入字段访问表单输入? - How can I access form input from an input field which was created dynamically via a Javascript function? 如何使用jquery验证动态填充的表单字段? - How do I use jquery to validate form fields, that was populated dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM