简体   繁体   English

如何从JavaScript中动态创建的html元素获取输入数据

[英]how to get input data from dynamically created html element usin javascript

I have a dynamically created fieldset with a form and 6 inputs for name, contact etc. I want get the data from these input fields using something like: 我有一个动态创建的字段集,该字段集具有一个表单和用于名称,联系方式等的6个输入。

document.getElementsByClassName("input-field")[0].id.value;

but no matter what method of accessing elements I use (ById, ByTagName etc) I always get the error cannot get value of undefined. 但是无论我使用哪种方法访问元素(ById,ByTagName等),我总是会收到错误,无法获取未定义的值。 Why is it undefined when I have 6 input fields with same class name and how can I access the value? 当我有6个具有相同类名的输入字段时,为什么未定义?如何访问该值? Been googling for ages and can't find anything that helps. 谷歌搜索已经很久了,找不到任何有用的东西。

EDIT 编辑

heres my code 这是我的代码

 // get body tag var mainBody = document.getElementsByTagName("body"); // create array of input labels var inputNameValues = ["first-name", "last-name", "title", "health-num", "email", "telephone"]; var inputLabels = ["First Name", "Last Lame", "Title", "Health Authority Number", "Email", "Telephone (Optional)"]; // create the contact form var contactPageDiv = document.createElement("div"); contactPageDiv.setAttribute("id", "form-cointainer"); var fieldSet = document.createElement("fieldset"); fieldSet.setAttribute("id", "contact-form") var form = document.createElement("form"); form.setAttribute("onsubmit", validateForm()); fieldSet.appendChild(form); // create labels and input fields for (let i = 0; i < inputLabels.length; i++) { var label = document.createElement("label"); var bold = document.createElement("b"); var inputText = document.createTextNode(inputLabels[i]); bold.appendChild(inputText); label.appendChild(bold); form.appendChild(document.createElement("br")); var input = document.createElement("input"); input.setAttribute("class", "input-field"); input.setAttribute("type", "text"); input.setAttribute("id", inputNameValues[i]); // setup placeholder text, focus and required fields input.setAttribute("placeholder", inputLabels[i]); if (inputNameValues[i] === "first-name") { input.setAttribute("autofocus", "true"); } if (inputNameValues[i] !== "telephone") { input.setAttribute("required", "true"); } // add tool tip icon if (inputNameValues[i] === "health-num") { // create the img var img = document.createElement("img"); img.setAttribute("id", "question-mark"); img.setAttribute("src", "resources/questionmark.jpg"); img.setAttribute("alt", "question mark symbol"); // create the tool tip text box var textBox = document.createElement("div"); textBox.setAttribute("id", "tool-tip-text-box"); var paragraph = document.createElement("p"); textBox.appendChild(paragraph); var text = document.createTextNode("If you do not know your ZHA number, please contact your GP"); paragraph.appendChild(text); label.appendChild(textBox); img.onmouseover = function() { textBox.style.display = "block"; } img.onmouseout = function() { textBox.style.display = "none"; } label.appendChild(img); } form.appendChild(label); form.appendChild(input); } // create submit button var submitBtn = document.createElement("input"); submitBtn.setAttribute("id", "submit-btn"); submitBtn.setAttribute("type", "submit"); submitBtn.setAttribute("value", "Submit!"); form.appendChild(submitBtn); contactPageDiv.appendChild(fieldSet); // display the form on the page mainBody[0].appendChild(contactPageDiv); function validateForm() { console.log(document.getElementsByClassName("input-field")[0].value); } 

This is an X/Y problem 这是一个X / Y问题

Remove the () from form.setAttribute("onsubmit", validateForm()); form.setAttribute("onsubmit", validateForm());

It is executing immediately before the fields exist 它在字段存在之前立即执行

Also use eventListener 也使用eventListener

form.addEventListener("submit", validateForm);

And I recommend querySelector 我建议使用querySelector

 function validateForm(e) { e.preventDefault(); // remove when you have tested // takes the first - use querySelectorAll to get the rest console.log(document.querySelector(".input-field").value); } // get body tag var mainBody = document.getElementsByTagName("body"); // create array of input labels var inputNameValues = ["first-name", "last-name", "title", "health-num", "email", "telephone"]; var inputLabels = ["First Name", "Last Lame", "Title", "Health Authority Number", "Email", "Telephone (Optional)"]; // create the contact form var contactPageDiv = document.createElement("div"); contactPageDiv.setAttribute("id", "form-cointainer"); var fieldSet = document.createElement("fieldset"); fieldSet.setAttribute("id", "contact-form") var form = document.createElement("form"); form.addEventListener("submit", validateForm); fieldSet.appendChild(form); // create labels and input fields for (let i = 0; i < inputLabels.length; i++) { var label = document.createElement("label"); var bold = document.createElement("b"); var inputText = document.createTextNode(inputLabels[i]); bold.appendChild(inputText); label.appendChild(bold); form.appendChild(document.createElement("br")); var input = document.createElement("input"); input.setAttribute("class", "input-field"); input.setAttribute("type", "text"); input.setAttribute("id", inputNameValues[i]); // setup placeholder text, focus and required fields input.setAttribute("placeholder", inputLabels[i]); if (inputNameValues[i] === "first-name") { input.setAttribute("autofocus", "true"); } if (inputNameValues[i] !== "telephone") { input.setAttribute("required", "true"); } // add tool tip icon if (inputNameValues[i] === "health-num") { // create the img var img = document.createElement("img"); img.setAttribute("id", "question-mark"); img.setAttribute("src", "resources/questionmark.jpg"); img.setAttribute("alt", "question mark symbol"); // create the tool tip text box var textBox = document.createElement("div"); textBox.setAttribute("id", "tool-tip-text-box"); var paragraph = document.createElement("p"); textBox.appendChild(paragraph); var text = document.createTextNode("If you do not know your ZHA number, please contact your GP"); paragraph.appendChild(text); label.appendChild(textBox); img.onmouseover = function() { textBox.style.display = "block"; } img.onmouseout = function() { textBox.style.display = "none"; } label.appendChild(img); } form.appendChild(label); form.appendChild(input); } // create submit button var submitBtn = document.createElement("input"); submitBtn.setAttribute("id", "submit-btn"); submitBtn.setAttribute("type", "submit"); submitBtn.setAttribute("value", "Submit!"); form.appendChild(submitBtn); contactPageDiv.appendChild(fieldSet); // display the form on the page mainBody[0].appendChild(contactPageDiv); 

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

相关问题 如何从 JavaScript 中动态创建的元素中获取文本 - How do I Get Text from Dynamically created element in JavaScript 如何从JavaScript中动态创建的复选框和单选按钮获取输入? - How to get input from dynamically created checkboxes and radio buttons in javascript? 如何使用 vanilla JavaScript 从动态创建的输入字段中获取数据? - How can I get data from dynamically created input field using vanilla JavaScript? 如何从JavaScript中动态创建的输入框中获取值 - How to get values from dynamically created input box in javascript 如何使用 javascript 将多个 css 类添加到 html 元素 - how to add multiple css classes to a html element usin javascript 动态创建的 HTML 文件输入元素不发布数据 - dynamically created HTML file input element doesn't post data 如何使用Javascript从HTML中获取元素数据? - How to get the data from element in HTML with Javascript? 如何将数据从PHP动态传递到JavaScript(在动态创建的HTML元素中) - How to dynamically pass data from PHP to JavaScript (In dynamically created HTML elements) 如何将javascript对象连接到函数内动态创建的html元素 - how to connect a javascript object to a dynamically created html element inside a function 如何使用JavaScript动态删除html中创建的输入表单 - How to dynamically remove a input form created in html using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM