简体   繁体   English

如何使每个列表项的复选框分别起作用?

[英]How do I make the checkboxes work separately for each list item?

I've appended checkboxes to each appended list item. 我已将复选框附加到每个附加的列表项。 I'm trying to get each checkbox to work distinctly for each list item, but when I click the checkbox, all of the list items get struck through. 我试图让每个复选框对每个列表项都可以单独工作,但是当我单击复选框时,所有列表项都会被删除。 I am thinking the solution requires creating a for loop to create distinct id's for each appended list item, but I'm having trouble accomplishing this. 我在想该解决方案需要创建一个for循环来为每个附加的列表项创建不同的ID,但是我很难做到这一点。 Any guidance would be greatly appreciated. 任何指导将不胜感激。 Thank you. 谢谢。

var inputtedChoreVar= document.getElementById("inputtedChore");
var toDoListVar= document.getElementById("toDoList");

var toDoArray= [];

document.getElementById('subButton').addEventListener('click',hitSubmit);

function hitSubmit() {

  let x= inputtedChoreVar.value;
  toDoArray.push(x);
  console.log(toDoArray);

  localStorage.setItem('toDoKey', toDoArray);

  var newListElement= document.createElement('LI');
  var newTextNode= document.createTextNode(x);
  newListElement.appendChild(newTextNode);
  toDoListVar.appendChild(newListElement);

  var fButtonElement= document.createElement('input');
  fButtonElement.setAttribute('type','checkBox');
  newListElement.appendChild(fButtonElement);
  fButtonElement.onclick= strike;

  function strike() {
    document.getElementById('toDoList').style.textDecoration='line-through';
  }
}

you should apply the style to the li element and not the whole list. 您应该将样式应用于li元素,而不是整个列表。

function strike() {
    newListElement.style.textDecoration='line-through';
  }

Your strike() function is setting a style on the entire toDoList element. 您的toDoList strike()函数正在整个toDoList元素上设置样式。 You probably want to scope that to your LI . 您可能希望将其范围限制到您的LI

var inputtedChoreVar= document.getElementById("inputtedChore");
var toDoListVar= document.getElementById("toDoList");

var toDoArray= [];

document.getElementById('subButton').addEventListener('click',hitSubmit);

function hitSubmit() {

  let x= inputtedChoreVar.value;
  toDoArray.push(x);
  console.log(toDoArray);

  localStorage.setItem('toDoKey', toDoArray);

  var newListElement= document.createElement('LI');
  var newTextNode= document.createTextNode(x);
  newListElement.appendChild(newTextNode);
  toDoListVar.appendChild(newListElement);

  var fButtonElement= document.createElement('input');
  fButtonElement.setAttribute('type','checkBox');
  newListElement.appendChild(fButtonElement);
  fButtonElement.onclick= function() { strike(newListElement); } // function that calls "strike" with an argument
}

function strike(el) {
  el.style.textDecoration='line-through';
}

I took the time to rewrite your code in the following way: 我花了一些时间通过以下方式重写您的代码:

Your first issue was that you was assigning the text-decoration property to the toDoListElement and each children was inheriting it. 您的第一个问题是,您正在将text-decoration属性分配给toDoListElement而每个子代都继承该属性。

You don't need to loop or to assign multiple different ids because JavaScript will save variable references for you (if you use the local variable newListElement inside your event handler, when the handler is executed it will use the last value that the variable had). 您不需要循环或分配多个不同的ID,因为JavaScript会为您保存变量引用(如果您在事件处理程序中使用局部变量newListElement ,则在执行处理程序时,它将使用该变量具有的最后一个值) 。

 let inputElement = document.getElementById("inputElement"); let toDoListElement = document.getElementById("toDoList"); document.getElementById('subButton').addEventListener('click', hitSubmit); function hitSubmit() { let newListElement = document.createElement('li'); let checkboxElement = document.createElement('input'); checkboxElement.setAttribute('type','checkbox'); checkboxElement.addEventListener('change', function strike(e) { if (e.target.checked) { newListElement.style.textDecoration = 'line-through'; } else { newListElement.style.removeProperty('text-decoration') } }); checkboxElement.style.marginLeft = '10px'; newListElement.innerText = inputElement.value; newListElement.appendChild(checkboxElement); toDoListElement.appendChild(newListElement); } 
 <input id="inputElement"></div> <button id="subButton" type="button">ADD</button> <div id="toDoList"></div> 

Finally, for the localStorage part, I didn't include it in the snippet, but I would use the following: 最后,对于localStorage部分,我没有在代码段中包含它,但是我将使用以下内容:

localStorage.setItem('toDoKey', JSON.stringify((JSON.parse(localStorage.getItem('toDoKey')) || []).concat([inputElement.value])));

You was passing the array directly to localStorage.setItem() which only accepts string values (for that you use JSON.stringify() and JSON.parse() ) 您将数组直接传递到仅接受字符串值的localStorage.setItem() (为此您使用JSON.stringify()JSON.parse()

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

相关问题 如何使同样使用列表项创建的按钮起作用? 我希望它删除数组的当前项。 每个项目都有自己的按钮 - How do i make the button that's also created with the list item work? I want it to delete the current item of the array. Each item has it own button jQuery:如何使全选适用于复选框 - jQuery: How do I make the selectall work for checkboxes 正在编写一个程序,该程序将物品制作成购物清单。 我必须将它们分别存储为变量 item1、item2、item3 和 output 每个项目 - Working on a program that takes items to make a shopping list. I must store them as variables item1,item2,item3 and output each item separately 如何让{{#each}}使用这些模板? - How do I make {{#each}} work with these templates? 如何分别显示状态的每个项目的点击次数? - How can I display the number of clicks for each item of a state separately? 为了使每个 class 的效果单独工作(Javascript) - To make the effect work separately for each class (Javascript) 使功能分别作用于每个元素 - Make function work on each element separately 如何让复选框与 React 一起使用? - How do I get checkboxes to work with React? 如何将处理程序绑定到列表中的每个项目? - How do I bind a handler to each item in the list? 如何使用 JavaScript 更改每个列表项的背景颜色? - How do I change background color of each list item with JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM