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

182
Views
No se puede establecer la opción seleccionada desde JavaScript

Tengo un problema al intentar configurar la opción seleccionada desde javascript. Creé una selección y agregué opciones con php de mysql así:

 <select class="form-control input-group-lg" name="progress" id="progress"> <?php if ($conn === false) { die("ERROR: Could not connect. " . mysqli_connect_error()); } $sql = "SELECT * FROM progress"; if ($result = mysqli_query($conn, $sql)) { if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result)) { $id = $row['id']; $progress_name = $row['name']; echo "<option>$progress_name</option>"; } } else { echo "<option>Fault!</option>"; } } else { echo "$sql. " . mysqli_error($conn); } mysqli_close($conn); ?> </select>

Y luego configure la opción seleccionada de javascript de esta manera:

 $('#progress').val("Value to set");

Este método funciona bien y establece la opción seleccionada con el valor que quiero.

OTRO MÉTODO:

Tengo un archivo php y selecciono datos de la base de datos (MySQL) en json y luego uso ajax como este

 $.ajax({ url: 'data_control/orders/progress_select.php', type: 'get', method: "POST", data: {selected_value:selected_value}, dataType: 'JSON', success: function (response) { var len = response.length; for (var i = 0; i < len; i++) { var progress_name = response[i].progress_name; var option = document.createElement("option"); option.text = progress_name; option.value = progress_name; var select = document.getElementById('progress'); select.appendChild(option); } } });

Las opciones se agregan con éxito, pero no puedo establecer el valor seleccionado de esta manera:

 $('#progress').val("Value to set");

¡Gracias por la ayuda!

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

0

No puede establecer el valor fuera del éxito.

O hazlo

 var select = document.getElementById('progress'); for (var i = 0; i < len; i++) { var progress_name = response[i].progress_name; var option = document.createElement("option"); option.text = progress_name; option.value = progress_name; select.appendChild(option); } select.value='Value to set'

O

 var select = document.getElementById('progress'); for (var i = 0; i < len; i++) { var progress_name = response[i].progress_name; var option = document.createElement("option"); option.text = progress_name; option.value = progress_name; if (progress_name==='Value to set') options.selected = true; select.appendChild(option); }

Alternativa más corta

 document.getElementById('progress').innerHTML = response .map(({progress_name}) => `<option${progress_name === 'Value to set'?' selected' : ''} value="${progress_name}">${progress_name}</option>`) .join("")
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!