【发布时间】:2020-02-03 21:35:49
【问题描述】:
我有一个问题。我正在制作测验应用程序,但我不知道如何在单击后从按钮中删除单击事件。所以我有 4 个按钮,其中 2 个的值为 1,所以这将是正确的选择。当我点击这个值为 1 的按钮时,它只需要加 1 分即可得分,但我的代码在每次点击后加一分,我需要它,这样如果你点击了正确的选项,你就不能点击这个按钮,但你可以在另一个按钮上。我需要对每个值为 1 的按钮执行此操作。我不想禁用该按钮!
class Quiz extends React.Component {
constructor(props) {
super(props);
this.state = {score: 0}
this.checkBtn = this.checkBtn.bind(this);
}
checkBtn(e) {
if (e.target.value == "1") {
this.setState({score: this.state.score+1});
this.removeEventListener('click', this.checkBtn, true); // here i need remove listener
}
}
render() {
return (
<div>
<button value="1" onClick={this.checkBtn}>first</button>
<button value="2" onClick={this.checkBtn}>second</button>
<button value="1" onClick={this.checkBtn}>first</button>
<button value="2" onClick={this.checkBtn}>second</button>
<p>{this.state.score}</p>
</div>
);
}
}
const Root = document.getElementById("root");
ReactDOM.render(<Quiz />, Root);
【问题讨论】:
-
禁用按钮有什么问题?
-
@Christopher Ngo 起初它不起作用,然后我说我不想使用禁用
标签: reactjs