javascript 单选按钮在每个li元素上相乘

bgtovc5b  于 2023-01-08  发布在  Java
关注(0)|答案(2)|浏览(170)

我试着做一个做应用程序。我想为我创建的每个li元素添加一个单选按钮。但我的问题是,每次我创建一个新的li元素与输入单选按钮乘法。我想只有一个按钮,为每个li元素创建。有人能帮助我吗?

// Select the input field
const inputField = document.querySelector('#input-field');

// Add an event listener to the input field that listens for the keydown event
inputField.addEventListener('keydown', (event) => {
  // Check if the enter key was pressed
  if (event.keyCode === 13) {
    // Get the value of the input field
    const inputFieldValue = inputField.value;

    // Create a new li element
    const liElement = document.createElement('li');

    // Set the li element's value to the value of the input field
    liElement.textContent = inputFieldValue;

    // Append the li element to an ul element
    const ulElement = document.querySelector('#list');
    ulElement.appendChild(liElement);

    // Select all the li elements
    const liElements = document.querySelectorAll('li');

    // Iterate through the li elements
    liElements.forEach((liElement) => {
      // Create a new input element
      const inputElement = document.createElement('input');

      // Set the type attribute of the input element to 'radio'
      inputElement.setAttribute('type', 'radio');

      // Set the name, value, and checked attributes of the input element
      inputElement.setAttribute('name', 'radio-group');
      inputElement.setAttribute('value', liElement.textContent);

      // Append the input element to the li element
      liElement.appendChild(inputElement);
    })}});
<div class="bg-light">
    <h1>To Do</h1>
    <input type="text"  id="input-field">
    <div class="task-list">
    <ul id="list"></ul>
</div>
irtuqstp

irtuqstp1#

也许这会有所帮助:

// Select the input field
const inputField = document.querySelector('#input-field');

// Add an event listener to the input field that listens for the keydown event
inputField.addEventListener('keydown', (event) => {
  // Check if the enter key was pressed
  if (event.keyCode === 13) {
    // Get the value of the input field
    const inputFieldValue = inputField.value;

    // Create a new li element
    const liElement = document.createElement('li');

    // Set the li element's value to the value of the input field
    liElement.textContent = inputFieldValue;

    // Append the li element to an ul element
    const ulElement = document.querySelector('#list');
    ulElement.appendChild(liElement);

    // Select all the li elements
    const liElements = document.querySelectorAll('li');

    // Iterate through the li elements
    // liElements.forEach((liElement, index) => {
    
    // you can limit just for last item
    //  if (index == liElements.length -1) {
        // Create a new input element
        const inputElement = document.createElement('input');

        // Set the type attribute of the input element to 'radio'
        inputElement.setAttribute('type', 'radio');

        // Set the name, value, and checked attributes of the input element
        inputElement.setAttribute('name', 'radio-group');
        inputElement.setAttribute('value', liElement.textContent);

        // Append the input element to the li element
        // Use last item only
        liElements[liElements.length-1].appendChild(inputElement);
    //  }
    // })
    
    }
    
    });
<div class="bg-light">
    <h1>To Do</h1>
    <input type="text"  id="input-field">
    <div class="task-list">
    <ul id="list"></ul>
</div>
qacovj5a

qacovj5a2#

您可以删除“forEach”循环,因为您只需要将单选按钮添加到新创建的列表项中,而不是所有列表项。

liElements.forEach((liElement) => { // remove this forEach loop

并将单选按钮直接添加到li

// Append the input element to the li element
  liElement.appendChild(inputElement);

查看此处创建的jsfiddle-https://jsfiddle.net/wq0nmkrd/

相关问题