简体   繁体   English

获取动态创建元素的 id 和 value

[英]Get id and value of dynamically created elements

I'm trying to get the ids of dynamically created elements, now these elements are in the form of input fields that can be modified by the user (the user enters the number of players then he gets input fields to type in their names), I'm trying to get their values as well, here is what I'm trying to do:我正在尝试获取动态创建的元素的 ID,现在这些元素采用用户可以修改的输入字段的形式(用户输入玩家的数量,然后他获取输入字段以输入他们的姓名),我也试图获得他们的价值观,这就是我想要做的:

// Creating the input fields
const x = localStorage.getItem('playersNum');
const parentDiv = document.getElementById('player-list');
for (let i = 0; i < x; i++) {
  const newInput = document.createElement("INPUT");
  newInput.setAttribute("type", "text");
  newInput.setAttribute("class", "form-control");
  newInput.setAttribute("id", `player${i}`);
  newInput.setAttribute("placeholder", "Player's Name");

  parentDiv.appendChild(newInput);
}

// Trying to access them
const players = document.getElementById('player-list').children;
console.log(players.value);

There are a number of ways to go about it: go 有很多方法可以了解它:

  1. Add the inputs to an array as you create them在创建输入时将输入添加到数组
  2. Add a unique class to each player input then use document.querySelectorAll('.className')为每个玩家输入添加一个唯一的 class 然后使用document.querySelectorAll('.className')
  3. Select the inputs based on inputs with ID starting with "player" Select 基于 ID 以“玩家”开头的输入的输入

Eg the following just adds "Player i" as the value of each input, but it could do other things.例如,以下只是添加“Player i”作为每个输入的值,但它可以做其他事情。

 // Creating the input fields // const x = localStorage.getItem('playersNum'); let x = 3; let parentDiv = document.getElementById('player-list'); for (let i = 0; i < x; i++) { let newInput = document.createElement("INPUT"); newInput.setAttribute("type", "text"); newInput.setAttribute("class", "form-control"); newInput.setAttribute("id", `player${i}`); newInput.setAttribute("placeholder", "Player's Name"); parentDiv.appendChild(newInput); parentDiv.appendChild(document.createElement('br')); } // Get a static NodeList of inputs with ID starting with "player" let players = document.querySelectorAll('input[id^=player]'); // Iterate over list, adding a value players.forEach((player, i) => player.value = 'Player ' + i);
 div {border: 1px solid #aaaaaa}
 <div id="player-list"> </div>

You can reduce your code somewhat by:您可以通过以下方式减少代码:

  1. The default type is "text" so no need to set it默认类型是“文本”,所以不需要设置它
  2. Set attributes using property access使用属性访问设置属性
  3. Append inputs as they're created Append 输入创建时

so:所以:

let newInput = parentDiv.appendChild(document.createElement("INPUT"));
newInput.class = 'form-control';
newInput.id = `player${i}`;
newInput.placeholder = 'Player\'s Name';

I'm not a fan of using a place holder as the label, much better to have an actual label and use the placeholder as a hint, so:我不喜欢使用占位符作为 label,最好有一个实际的 label 并使用占位符作为提示,所以:

<label for="player0">Name player 0:<input id="player0" class="form-control"></label>

or similar.或类似的。

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

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