简体   繁体   English

如何在reactjs中取消选中复选框时清空值?

[英]how to empty value on unchecking checkbox in reactjs?

I'm new to reactjs. 我是ReactJS的新手。 I have created below code to make checkbox and to get value of checkbox. 我创建了下面的代码来制作复选框并获取复选框的价值。 But on unchecking the checkbox I'm still getting the value. 但是,取消选中该复选框,我仍然会获得价值。 How to remove value of unchecking the checkbox. 如何删除取消选中复选框的值。 Please help. 请帮忙。

below is in html 下面是HTML

<input onChange={(event)=>this.getCheckboxValue(event)} type='checkbox' value={item.title}/>

Below is function 下面是功能

 getCheckboxValue(event) {
   console.log(event.target.value)
 }

The value of the checkbox doesn't change. 复选框的值不变。 You're interested in whether it's checked ( event.target.checked ) in addition to its value ( event.target.value ). 除了其值( event.target.value )之外,您还对是否选中( event.target.checked )感兴趣。

<input onChange={this.onChange} type='checkbox' value={item.title}/>
onChange = (event) => {
  const {checked, value} = event.target;
  if (checked) {
    // do something with value
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM