亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

LitElement不更新列表中的復選框

LitElement不更新列表中的復選框

大話西游666 2021-05-03 17:43:52
我有一個簡單的清單,其中每個項目都有一個刪除按鈕。當我檢查第一個項目然后將其刪除時,列表會更新,刪除該項目,但會選中下一個項目的復選框。下一項的屬性正確。這是我的代碼:import { LitElement, html } from 'lit-element';class CheckList extends LitElement {  static get properties() {    return {      items: { type: Array },    };  }  constructor() {    super();    this.items = [      {        id: 1,        text: 'Item 1',        isDone: false,      },      {        id: 2,        text: 'Item 2',        isDone: false,      },    ];    this.toggleCheck = this.toggleCheck.bind(this);    this.deleteItem = this.deleteItem.bind(this);  }  render() {    return html`      <ul>        ${this.items.map(item => html`          <li>            <input              type="checkbox"              value=${item.id}              ?checked=${item.isDone}              @click=${this.toggleCheck}            >            ${item.text}            <button @click=${this.deleteItem}>X</button>          </li>        `)}      </ul>    `;  }  toggleCheck(e) {    const id = Number(e.target.value);    this.items = this.items.map(item => {      if (item.id === id) {        item.isDone = !item.isDone;      }      return item;    });  }  deleteItem(e) {    const id = Number(e.target.parentNode.querySelector('input').value);    this.items = this.items.filter(item => item.id !== id);  }}customElements.define('check-list', CheckList);https://stackblitz.com/edit/typescript-fylwxb
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號