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

230
Views
Establezca el valor predeterminado en la lista desplegable usando jQuery

Tengo muchas opciones en mi lista desplegable como:

 <option value="1">it's me</option>

Necesito seleccionar la opción que tiene valor soy yo dentro de la etiqueta, no por atributo como 1 .

¿Cómo puedo hacer esto usando jQuery?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

si desea usar jQuery para esto, intente con el siguiente código.

 $('select option[value="1"]').attr("selected",true);

Actualizado:

Después de un comentario de Vivek, señaló correctamente que Steven Spielberg quería seleccionar la opción a través de su valor de Texto.

A continuación se muestra el código actualizado.

 $('select option:contains("it\'s me")').prop('selected',true);

Debe usar el selector :contains(text) para buscar a través del texto que lo contiene.

También jQuery prop ofrece un mejor soporte para Internet Explorer al obtener y configurar atributos.

Un ejemplo de trabajo en JSFiddle

over 4 years ago · Santiago Trujillo Report

0

Puedes hacer esto:

 $('#myCombobox').val(1)
over 4 years ago · Santiago Trujillo Report

0

val() debería manejar ambos casos

 <option value="1">it's me</option> $('select').val('1'); // selects "it's me" $('select').val("it's me"); // also selects "it's me"
over 4 years ago · Santiago Trujillo 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!