繁体   English   中英

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

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

但是,当我尝试从动态创建的表单字段中获取值时,我从表单提交中获取了空值,但没有使用html中创建的文本字段。 任何帮助,将不胜感激。

小代码段。

            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";

        }

下面的代码可完美地提交动态创建的字段。

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:

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);
});

如果由于某种原因对您不起作用,您是否会提交HTML和完整的javascript上下文?

暂无
暂无

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

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