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

200
Views
Cómo pasar valor de un <select> en javascript

Estoy tratando de pasar el valor seleccionado de <select> en la función onChange . Intenté esta pregunta SO, pero this y this.value no funcionan. Sé que la pregunta es un poco vieja...

 setShippingMethod(obj) { console.log('Shipping method ' + JSON.stringify(obj)); this.state.defaultMethod = obj.value; this.props.setShippingMethod(obj.value); } render() { const shippingMethods = this.state.shippingMethods; const defaultMethod = this.state.defaultMethod; return ( <aside id="checkout" className="block col-1"> <h1>Shipping</h1> <div className="row"> <select id="comboMethod" onChange={setShippingMethod(this)} value={!!defaultMethod ? null : defaultMethod.trim()}> {shippingMethods.map(method => { return <option key={method.shipmthd.trim()} value={method.shipmthd.trim()}>{method.shipmthd.trim()}</option>} )} </select> </div> </aside> ); }

Mi línea console.log('Shipping method ' + JSON.stringify(obj)) genera un error "Error de tipo no capturado: valor de objeto cíclico" y el mensaje parece ser this todavía se refiere a todo el modal (en mi caso). ¿Hay una forma más nueva de hacer esto? Cualquier ayuda será apreciada.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Cuando usa eventos Listeners en reaccionar, obtiene un objeto de evento. Su Puede acceder al valor que ingresó al elemento con e.target.value.

 setShippingMethod(e) { console.log('Shipping method ' + JSON.stringify(e.target.value)); //...rest of the code } render() { const shippingMethods = this.state.shippingMethods; const defaultMethod = this.state.defaultMethod; return ( <aside id="checkout" className="block col-1"> <h1>Shipping</h1> <div className="row"> <select id="comboMethod" onChange={setShippingMethod} value={!!defaultMethod ? null : defaultMethod.trim()}> {shippingMethods.map(method => { return <option key={method.shipmthd.trim()} value={method.shipmthd.trim()}>{method.shipmthd.trim()}</option>} )} </select> </div> </aside> ); }
about 4 years ago · Juan Pablo Isaza Report

0

esto siempre va a ser una referencia a la clase mientras estás en clase. si desea pasar el valor de la opción seleccionada, necesita algo como

 onChange={(e) => {setShippingMethod(e.target.value)}}
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!