admin管理员组

文章数量:1279049

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;
}

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;
}
Share Improve this question edited Dec 20, 2019 at 17:38 Dharman 33.4k27 gold badges101 silver badges147 bronze badges asked Jun 5, 2017 at 9:10 P.MacsP.Macs 771 gold badge3 silver badges16 bronze badges 1
  • Pro tips for posting: (1) please do not offer voting advice. If people wish to vote on your posts, they will, and asking for people not to downvote will probably have the opposite effect; (2) try to refrain from "please help me" and other forms of begging and pleading - it's another good way to get downvotes. Experienced users find it annoying, since hundreds of people beg every single day. (3) When referring to yourself, please use a capital letter "I" - all-lower-case posting might be OK on Facebook, but readability is important here. Thanks! – halfer Commented Aug 21, 2017 at 17:52
Add a ment  | 

3 Answers 3

Reset to default 4

First, dont check onSubmit but on change (you dont want to disable it AFTER it was submitted) for both your input. Add a state value for your disabledButton, and set it to false only if both input are the same (in the onChange event).

You can set disabled button inside your JSX like this

<button disabled={this.state.disabledButton} />

You can do that in your render function:

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>
    // ...
  )
}

本文标签: javascriptHow to disable button onclick in ReactjsStack Overflow