简体   繁体   English

如何使用使用Javascript动态创建的输入字段?

[英]How to work with input field that is created dynamically using Javascript?

I know how to work with input field using Javascript. 我知道如何使用Javascript处理输入字段。 But when I create a input field dynamically using javascript createElement method, I am not being able to work with that. 但是,当我使用javascript createElement方法动态创建输入字段时,无法使用它。 As I have not started learning PHP, I want to know how to work with dynamically created input field using javascript. 由于尚未开始学习PHP,因此我想知道如何使用javascript处理动态创建的输入字段。

If you add an Element using createElement You can then work with the element by referring to the element by ID or Class. 如果使用createElement添加元素,则可以通过按ID或类引用该元素来使用该元素。

document.getElementById()

document.getElementsByClassName()

Take a look at MDN site for examples 看看MDN网站上的示例

Probably easiest way to track (and manipulate) the input fields is with the getElementsByTagName(); 跟踪(和操作)输入字段的最简单方法是使用getElementsByTagName(); which returns an array of all the elements with the specified tag . 返回带有指定标签的所有元素的数组

So, as an example: 因此,例如:

 // saves all current elements in an array var inputsArr = document.getElementsByTagName('input'); console.log('Current input fields: ' + inputsArr.length); // now let's create our new Element and set its attributes! var newinput = document.createElement('input'); newinput.setAttribute('type', 'button'); newinput.setAttribute('value', 'Submit form'); // adds (appends) the newly created input document.getElementById('structure').appendChild(newinput); console.log('-- button added'); var newinputsArr = document.getElementsByTagName('input'); console.log('Current input fields: ' + newinputsArr.length); // and we can manipulate with the newly added element newinputsArr[2].setAttribute('style', 'background:gold;'); 
 input[type="button"]{ display: block; margin-top: 10px; } #structure { background: lightgray; height: 70px; } 
 <form id="structure"> Login: <input type="text"> Password: <input type="password"> </form> 

In the snippet below I showcased 在下面的代码段中,我展示了

  1. How to fill an array with existing input elements 如何用现有的输入元素填充数组
  2. How to add a new input item and then access it 如何添加新的输入项然后访问它
  3. How to manipulate with the newly added element 如何使用新添加的元素进行操作

Note, I intentionally created two separate arrays, as a showcase. 注意,我有意创建了两个单独的数组作为展示。 So if you meant to manipulate eg. 因此,如果您打算操纵例如。 with only newly added elements, you can apply the the expression only to the 'extra' elements of the difference between newinputsArr.length and inputsArr.length 仅使用新添加的元素,您可以将表达式仅应用于newinputsArr.lengthinputsArr.length之间差异的“额外”元素

You probably forgot to add the input field to the body of the document. 您可能忘记了将输入字段添加到文档正文中。 When you create an element with document.createElement , never forget to append it to the body when you are done. 使用document.createElement创建元素时,请不要忘记在完成后将其附加到主体。

var input = document.createElement("input");
document.body.appendChild(input);

 <span id="result"></span><br/> <script> var input = document.createElement("input"); input["type"] = "text"; document.body.appendChild(input); input.addEventListener("keypress", function(e){ document.getElementById('result').textContent = "Key: "+e.key+" KeyCode: "+e.keyCode+" Value: "+this.value+e.key; }); </script> 

暂无
暂无

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

相关问题 如何动态删除使用JavaScript动态创建的输入字段 - How to dynamically remove input field which is created dynamically using JavaScript 如何使用javascript插入和获取动态创建的输入字段的值? - How to insert and get the value of a dynamically created input field using javascript? 如何使用 vanilla JavaScript 从动态创建的输入字段中获取数据? - How can I get data from dynamically created input field using vanilla JavaScript? 如何使用JavaScript动态删除html中创建的输入表单 - How to dynamically remove a input form created in html using javascript 如何使用javascript / jQuery将值分配给动态创建的输入框 - How to assign value to dynamically created input box using javascript/Jquery 如何从通过 Javascript function 动态创建的输入字段访问表单输入? - How can I access form input from an input field which was created dynamically via a Javascript function? 如何在JavaScript,PHP响应的ajax中动态创建的输入字段中更新特定的输入字段? - How to update a particular input field among dynamically created input fields in JavaScript, ajax from PHP response? 隐藏使用 JavaScript 动态创建的输入按钮 - Hiding input button that was dynamically created using JavaScript 如何使用Javascript / Jquery动态添加输入字段的名称属性 - How to add name attribute of input field dynamically using Javascript/Jquery 如何使用本机JavaScript将跨度动态添加到输入字段 - How to append Span to input field dynamically using native javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM