Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Cambiar input.value desde la función onchange (Reactjs)

Tengo una entrada ligada al estado. HD variable. Pero quiero agregar validación para que si se ingresa un número negativo, onblur el valor de la entrada se establece en el valor anterior de HD.

Simplemente no sé cómo llamar a la entrada y hacer que cambie su valor. Traté de consultarlo aquí para un intento descontrolado de cambiarlo ( https://www.pluralsight.com/guides/how-to-use-react-to-set-the-value-of-an-input )

Prestar

 <input className="input" name="HDin" type="number" defaultValue={this.state.HD} onBlur={this.changeHD} ref={this.HDref} ></input>
 changeHD = (e) => { let temp = this.state.HD; const amnt = e.target; let o = 0; o = parseInt(e.target.value); console.log(o); if (o < 0 || o === "" || o == NaN) { this.setState({ HD: temp }); this.HDref.value = temp; this.setState({[e.target.value]: temp}); } else { this.setState({ HD: o }); } };
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

input value={this.state} entonces puedes cambiarlo usando this.setstate

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar setState y prevState que está disponible.

 class Counter extends React.Component { state = { counter: 0 }; onChangeHandler = (e) => { const currentValue = e.target.value; if (currentValue <= -1) { this.setState((prevState, props) => { return { counter: prevState.counter }; }); } else { this.setState({ counter: currentValue }); } }; render() { return ( <div> {this.state.counter} <input type="text" onBlur={this.onChangeHandler} /> </div> ); } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!