javascript - 单选按钮不允许我切换反应
问题描述
class Actor extends Component {
constructor(props){
this.handleCheck = this.handleCheck.bind(this);
}
state={
voices : [
{"Voice":"Indian English Female Voice 1"},
{"Voice":"Indian English Female Voice 2"},
{"Voice":"Indian English Male Voice 1"}
],
checkedElement:""
}
handleCheck(){
this.setState({checkedElement:event.target.value})
}
render(){
return(
{voices.map((item,index)=>{
return(
<label key={index} htmlFor = {item.Voice}>
{item.Voice}
<input
key = {index+1}
value={item.Voice}
type = "radio"
onChange ={this.handleCheck}/>
</label>
)
})}
)
}
}
这显示为三个选项,但是一旦我选中其中一个,当我切换到下一个选项时,它不会取消选中第一个。它只是保持选中状态,因此不会使其成为唯一的选择。
解决方案
对于上面的代码,你可以添加一个属性'name'来输入,就像
<input
name={'item'}
key = {index+1}
value={item.Voice}
type = "radio"
onChange ={this.handleCheck}
/>.
您还可以从链接中查看示例。
推荐阅读
- powerbi - 日期PowerBI的累计发生次数?
- oracle - 如何解决这个关于 Oracle 触发器的练习
- javascript - Javascript 从此数组中追加对象并创建带有对象键值对的新数组
- javascript - 无法提交包含多个文件和输入的表单
- android - 我正在尝试为我的应用程序创建启动画面,但我收到 AAPT: error: not well-formed error
- node.js - 如何从 GET 请求中获取 pdf 文件并从中获取文本?
- node.js - 类型ORM getRawOne
不返回类型 T - oracle - 如何在 PL/SQL 中使用包内的过程
- django - Django:“'Blogmodel' 对象不可迭代”
- c# - Windows 蓝牙 LE 连接速度