
BUCLE FOR DENTRO DE UN SELETC
Publicado por Miriam (6 intervenciones) el 24/11/2022 14:22:29
Hola.
Necesito meter un for que tengo hecho dentro de un select:
Ahora los ingredientes me salen así:

Pero necesito mostrar los ingredientes en un select en lugar de que aparezcan checkbox y al lado un input de texto libre para la cantidad. Además también necesito poner un botón añadir para ir añadiendo más ingredientes.
Si le doy al botón me mostrará otro select con otro input al lado para la cantidad.
La idea es que salga algo así:

He intentado mil formas pero me da error, ¿alguien me puede ayuda?
Había pensado algo así pero no consigo montarlo según mi código:
Gracias, saludos.
Necesito meter un for que tengo hecho dentro de un select:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Consulta a la API para obtener los tipos de recetas
//Forma de atacar al servicio REST
let xmlHttp = new XMLHttpRequest();
xmlHttp.open(
"GET",
"http://localhost/recetarioappweb/sistema/API/ingredientes.php",
false
); // false for synchronous request
xmlHttp.send(null);
//console.log(xmlHttp.responseText);
//Convertimos en objeto
let resultadoIngredientes = JSON.parse(xmlHttp.responseText);
let html = "<label>Seleccione uno o varios ingredientes: </label> <br><br>";
for (let i = 0; i < resultadoIngredientes.length; i++) {
html += `<div class='form-check form-check-inline'>
<input class='form-check-input' type='checkbox' value='${resultadoIngredientes[i].id}' id='inlineCheckbox1' name='ingredientes[]'>
<label class='form-check-label' for='inlineCheckbox1'>
${resultadoIngredientes[i].nombre}
</label>
</div>`;
}
document.getElementById("ingredientes").innerHTML += html;
Ahora los ingredientes me salen así:
Pero necesito mostrar los ingredientes en un select en lugar de que aparezcan checkbox y al lado un input de texto libre para la cantidad. Además también necesito poner un botón añadir para ir añadiendo más ingredientes.
Si le doy al botón me mostrará otro select con otro input al lado para la cantidad.
La idea es que salga algo así:

He intentado mil formas pero me da error, ¿alguien me puede ayuda?
Había pensado algo así pero no consigo montarlo según mi código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
!empty($_POST) && var_dump($_POST);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Crear/Editar receta</h1>
<form method="post">
Nombre: <input name="nombre_receta"><br />
(Otros campos de la receta)
<h2>Añadir ingredientes:</h2>
<button id="add_ingrediente">Añadir</button>
<div id="form_ingredientes">
</div>
<div><input type="submit" value="Enviar"></div>
</form>
<script>
let cont = 0;
document.querySelector('#add_ingrediente').addEventListener('click', function (event) {
event.preventDefault();
text = '<div>\
<div style="display: inline">\
<select name="ingredientes['+cont+'][\'id_ingrediente\']" id="">\
<option value="">Selecciona ingrediente</option>\
<option value="1">Leche</option>\
<option value="2">Huevo</option>\
<option value="3">Arroz</option>\
</select>\
</div>\
<div style="display: inline">\
<input name="ingredientes['+cont+'][\'cantidad\']" type="text">\
</div>\
</div>';
document.querySelector('#form_ingredientes').insertAdjacentHTML("beforeend", text);
cont++;
});
</script>
</body>
</html>
Gracias, saludos.
Valora esta pregunta


0