迭代todos数组。内部的对象具有isChecked属性。如果isChecked === true标记复选框,则如果isChecked === false复选框为uncheckbox。当我单击复选框时。我无法标记或取消选中

此处演示:https://stackblitz.com/edit/react-ds9rsd

class App extends Component {
  constructor() {
    super();
    this.state = {
      todos: [
        {
          name:'A',
          id: 1,
          isChecked: true
        },
         {
          name:'B',
          id: 2,
          isChecked: false
        },
         {
          name:'C',
          id: 3,
          isChecked: true
        }
      ]
    };
  }

  checked = (e) => {
    console.log(e.target.checked)
  }

  render() {
    return (
      <div>
        {this.state.todos.map((todo, index) => {
          return <input type="checkbox" checked={todo.isChecked} onChange={(e) => this.checked(e)}/>
        })}
      </div>
    );
  }
}

最佳答案

在checked()函数中,您只是在记录值。相反,您需要执行setState()来保存新状态。

关于javascript - 单击后复选框不希望选中或取消选中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57993580/

10-11 23:35