javascript - 如何使用 javascript 访问 li 文本来创建新列表

标签 javascript list function append

我无法让这个功能正常工作。它只列出食物列表的第一个单词(默认情况下是食物),如果我从食物列表中选择多个项目,它不会为每个选择创建一个新的 li。我究竟做错了什么?

到目前为止,我发现的最好方法是将列表字符串拆分为数组,然后将 0 索引 append 到 searchList ul。但它只会列出食物列表中的第一项。

function addToList(){
  let food = document.getElementById("food").value;
  let amount = document.getElementById("amount").value;
  let unit = document.getElementById("unit").value;



  let li = document.createElement("li");
    li.textContent = food + ' ' + amount + ' ' + unit + '.';
    document.getElementById("foodlist").appendChild(li);

  let checkbox = document.createElement('input');
    checkbox.type = "checkbox";
    checkbox.value = 1;
    li.appendChild(checkbox);
};


function addToPantry () {
  for (i = 0; i<foodlist.length; i++){
    let name = foodlist[0];
    pantry.push(`${name}: [${amount}, [${unit}]]`)
  }


}

function addToSearch() {
  let list = document.getElementById("foodlist").innerText;
  let split = list.split(" ")

  if (list.checked = true){
    let li = document.createElement('li');
      li.textContent =split[0];
      document.getElementById("searchList").appendChild(li);
    }
  }
<input type="text" name="" value="food" id="food">
  <br><br>

<input type="text" name="" value="amount" id="amount">
  <br><br>

<select id="unit">
  <option value="oz">ounces</option>
  <option value="lb">pounds</option>
  <option value="servings">servings</option>
</select>
  <br><br>

<button  onclick ="addToList(), addToPantry()" type="button" name="button" id="addButton">add</button>

<ul id="foodlist"></ul>

<button onclick="addToSearch()" type="button" id="toSearch" name="button">Add To Search </button>

  </div>
<h3>You are searching for recipes containing:</h3>

<ul id="searchList"></ul>

最佳答案

除了错误的比较(这是一项赋值)之外,您还需要获取 foodlist 的元素并检查是否检查了一个输入。然后更新列表。

function addToList() {
  let food = document.getElementById("food").value;
  let amount = document.getElementById("amount").value;
  let unit = document.getElementById("unit").value;
  let li = document.createElement("li");
  li.textContent = food + ' ' + amount + ' ' + unit + '.';
  document.getElementById("foodlist").appendChild(li);

  let checkbox = document.createElement('input');
  checkbox.type = "checkbox";
  checkbox.value = 1;
  li.appendChild(checkbox);
}

function addToPantry() {
  for (i = 0; i < foodlist.length; i++) {
    let name = foodlist[0];
    pantry.push(`${name}: [${amount}, [${unit}]]`)
  }
}

function addToSearch() {
  let list = document.getElementById("foodlist").getElementsByTagName('li'),
      i;
      
  for (i = 0; i < list.length; i++) {
    if (list[i].querySelector('input').checked) { //
      let li = document.createElement('li');
      li.textContent = list[i].innerText;
      document.getElementById("searchList").appendChild(li);
    }
  }
}
<input type="text" name="" value="food" id="food">
<br><br>
<input type="text" name="" value="amount" id="amount">
<br><br>
<select id="unit">
  <option value="oz">ounces</option>
  <option value="lb">pounds</option>
  <option value="servings">servings</option>
</select>
<br><br>
<button onclick="addToList(), addToPantry()" type="button" name="button" id="addButton">add</button>
<ul id="foodlist"></ul>
<button onclick="addToSearch()" type="button" id="toSearch" name="button">Add To Search </button>
<h3>You are searching for recipes containing:</h3>
<ul id="searchList"></ul>

关于javascript - 如何使用 javascript 访问 li 文本来创建新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55648234/

相关文章:

c# - 识别List中的common Lists并返回List中的Distinct Lists

php - 需要修改 javascript 的帮助(将 php 值插入其中)

javascript - Vue 将新项目添加到对象属性数组中

python - 从空集和列表中弹出的不同异常(exception)?

java - 在java中创建一个字符串列表,其中每个字符串都是元组列表(作为值)的键

javascript - jQuery 或 JavaScript 中的双重延迟

javascript - 添加相对于对象的速度

javascript - REACT JSX - 链式条件

php - 此功能正在检查什么?

c - 如何在不使用c中的数组的情况下洗牌一副结构牌