[英]How to disable a checkbox after executing onChange method?
I want to be able to disable a checkbox once I check on the checkbox and execute the onChange method.我希望能够在选中复选框并执行 onChange 方法后禁用复选框。
I was able to find a way to do this, but once multiple items are checked only the recently checked checkbox is disabled.我能够找到一种方法来执行此操作,但是一旦选中多个项目,则只会禁用最近选中的复选框。
The disable method is inside a component class before the render method disable 方法位于 render 方法之前的组件类中
Disable = id => {
document.getElementById(id).disabled = true;
//another method to execute onchange
};
<span>
<input
type="checkbox"
className="form-check-input"
onChange={this.Disable}
value=""
/>
</span>;
The checkbox is inside the render method.该复选框位于渲染方法内。 One the user checks the checkbox the checkbox needs to check and disable it self
用户检查复选框需要检查和禁用它自己
Some notice points:一些注意点:
document.getElementById
in your current situationdocument.getElementById
id
, if you just need id
, pass it as a param this.handler(id)
id
,如果您只需要id
,请将其作为参数this.handler(id)
Demo:演示:
const App = () => { const [checked, setChecked] = React.useState(false); const [status, setStatus] = React.useState(true); const onChangeHandler = () => { setChecked(!checked); setStatus(false); }; return ( <div className="App"> <input type="checkbox" value={checked} disabled={!status} onChange={onChangeHandler} /> </div> ); }; ReactDOM.render(<App />, document.getElementById("root"));
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.production.min.js"></script>
you should have to attach id
on element .你应该在 element 上附加
id
。
let Disable=(id)=>{ if(id){ document.getElementById(id).disabled = true } }
<span> <input type="checkbox" className="form-check-input" onChange="Disable(this.id)" id="one" value=""/> <input type="checkbox" className="form-check-input" onChange="Disable(this.id)" id="two" value=""/> <input type="checkbox" className="form-check-input" onChange="Disable(this.id)" id="three" value=""/> </span>
passing the event
in an anonymous arrow function and accepting it as an argument
in the eventHandler(component method)
and using the event.target
there is the appropriate way of doing this in such situations that you need to access the caller.在匿名箭头函数中传递
event
并在eventHandler(component method)
接受它作为argument
并使用event.target
在需要访问调用者的情况下有适当的方法来执行此操作。
class Stack extends Component {
Disable = event => {
event.target.disabled = true;
//another method to execute onChange
};
render() {
return (
<span>
<input
type="checkbox"
className="form-check-input"
onChange={event => this.Disable(event)}
value=""
/>
<input
type="checkbox"
className="form-check-input"
onChange={event => this.Disable(event)}
value=""
/>
<input
type="checkbox"
className="form-check-input"
onChange={event => this.Disable(event)}
value=""
/>
</span>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.