[英]How to disable button onclick in Reactjs?
I had a condition if the two textfield are the same it will proceed, if not then the button is disabled.我有一个条件,如果两个文本字段相同,它将继续进行,如果不是,则按钮被禁用。 I know I'm doing this wrong.
我知道我做错了。
onSubmit() {
debugger
if (this.state.password == this.state.password2)
alert('same');
else
alert('error');
this.disable;
}
First, dont check onSubmit but on change (you dont want to disable it AFTER it was submitted) for both your input.首先,不要检查 onSubmit 而是检查更改(您不想在提交后禁用它)对于您的输入。 Add a state value for your disabledButton, and set it to false only if both input are the same (in the onChange event).
为 disabledButton 添加一个状态值,并仅在两个输入相同(在 onChange 事件中)时才将其设置为 false。
You can set disabled button inside your JSX like this您可以像这样在 JSX 中设置禁用按钮
<button disabled={this.state.disabledButton} />
You can do that in your render
function:你可以在你的
render
函数中做到这一点:
render() {
const submitDisabled = this.state.password !== this.state.password2
return (
// ...
<button disabled={submitDisabled}>Submit</button>
// ...
)
}
This will work Perfect.这将工作完美。 Try this
尝试这个
render() {
const submitDisabled = password.length > 0
return (
// ...
<button disabled={submitDisabled}>Submit</button>
// ...
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.