reactjs - React 在单选按钮选择上附加一个按钮
问题描述
我正在尝试在选择单选按钮时附加一个按钮。我知道我必须与状态一起玩才能实现这一点,但这对我来说很复杂。请帮忙。
解决方案
尝试这个:
class Radio extends React.Component {
constructor(props) {
super(props);
this.state = {radio: 0};
this.onRadioChanged = this.onRadioChanged.bind(this);
}
onRadioChanged(event) {
this.setState({radio: parseInt(event.target.value, 10)});
}
render() {
return (
<div>
<input type="radio"
value={0}
checked={this.state.radio === 0}
onChange={this.onRadioChanged} />
Option 1
<input type="radio"
value={1}
checked={this.state.radio === 1}
onChange={this.onRadioChanged} />
Option 2
{this.state.radio === 1? <button>Hi!</button> : null}
</div>
);
}
}
React.render(
<Radio/>,
document.body
);
推荐阅读
- javascript - 如何禁用过去的日期?
- python - 写入文本文件时出现问题。被覆盖/剪切的文本文件
- python - 使用 Beam IO ReadFromPubSub 模块时,可以在 Python 中拉取带有属性的消息吗?不清楚是否支持
- julia - 对 Julia、CuArrays 和 ArrayFire 中的 Nvidia GPU 包感到困惑
- mysql - 如何使用 mySQL 查询按顺序连接多个字段?
- javascript - 为什么我的内部函数会在纯 JavaScript 中导致“未捕获的 TypeError:countMe.Increment 不是函数”?
- sql - Oracle 连接子句中的无效数字
- bash - 试图删除我的 .git 文件夹并且 'rm -r .git --force' 不起作用
- javascript - flex的上下滑动功能问题:1
- mysql - mysql 理解 group by 与一个连接表