我有默认情况下被禁用的按钮。但是当复选框被选中时,按钮将被启用,使用 ref 我启用按钮。按钮激活后,单击事件不会触发。如果没有默认禁用,按钮的点击事件就会被触发。
class TodoApp extends React.Component {
onChangeCheckBox(event) {
if(event.target.checked) {
this.actionButtonRef.disabled = "";
}
else {
this.actionButtonRef.disabled = "disabled";
}
}
render() {
return (
<div>
<input
type="checkbox"
ref={element => {
this.checkboxAllRef = element;
}}
onChange={event => this.onChangeCheckBox(event)}/> Checbox
<br />
<button
type="button"
className="btn btn-danger"
ref={element => {
this.actionButtonRef = element;
}}
onClick={event => {
alert("clicked");
}}
disabled="disabled">Button</button>
</div>
)
}
}
ReactDOM.render(<TodoApp />, document.querySelector("#app"))