Estimados
Estoy intentando capturar un dato de una tabla html y enviarlo a un combo box, pero no me funciona, solo me funciona si lo envío a un campo input. he probado enviando el valor a la etiqueta del combo pero no muestra nada. aca les dejo un ejemplo que hice para poder explicar mi problema.
acá imagen de como funciona con input text.
acá dejo el código.
<!DocType:html>
<html>
<head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<!--<script type="text/javascript" src="/fechaHora.js"></script>-->
</head>
<body>
<h1>Formulario 1 capturar celda de tabla y mostrar en input text</h1>
<table border=1 cellpadding=0 id="tablatest">
<thead>
<tr>
<th>ID</th>
<th>NOMBRE</th>
</tr>
</thead>
<tbody>
<tr>
<td>7889</td>
<td>HOLA</td>
</tr>
<tr>
<td>7676</td>
<td>HOLA2</td>
</tr>
</tbody>
</table>
<br>
<form>
<div><b>Combo:</b></div>
<div class="col-sm-2"><select id="combo" class="form-control">
<option ></option>
</select>
</div>
</form>
<script>
$(document).ready(function () {
$("#tablatest tbody tr ").on ("click",function (event) {
var id2= $(this).find("td:nth-child(2)").html();
$("#combo").val(id2);
});
});
</script>
</body>
</html>
También probé pasando el valor por el id directo a la etiqueta pero tampoco me funciona. Como se podría pasar el valor al combo box?
option
con elvalue
que quieres poner.