I need to show a select
with a quantity of data from a table, and that on clicking I can load a value (the id
). I come from VB6
where it was quite easy, here I see that it has some more lap. For aesthetic reasons I got a code where the select
is replaced by a messy list ... and I liked it a lot more. The problem is that I do not understand it at all, and according to how I could do it, I managed to load the values except for the first one. The idea is that I can load a default value that is shown first, and then in the list all the values of that field are loaded. They help me understand and solve?
I copy the code:
<html>
<head>
<title>prueba combo</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link href="css/estilo.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script src="js/jquery-3.1.0.js" type="text/javascript"></script>
<script src="js/func.js" type="text/javascript"></script>
</head>
<body>
<?php include 'conexion.php'; ?>
<div id="contiene_combo">
<div class="cajaselect">
<?php
$consulta="select sector FROM 'maestros'";
$result=mysqli_query($con,$consulta);
$zzzz = $result->fetch_array(MYSQLI_ASSOC);
?>
<span class="seleccionado"><?php echo $zzzz['sector'][0]; ?></span>
<ul class="listaselect">
<?php
while ($filas=mysqli_fetch_array($result))
{
$dato=$filas['sector'];
?>
<li>
<a href="#"><?php echo $dato ?></a>
</li>
<?php };
?>
</ul>
<span class="trianguloinf"></span>
</div>
</div>
</body>
</html>
And I also copy the .js:
$(document).ready(function() {
function clickcaja(e) {
var lista = $(this).find("ul"),
triangulo = $(this).find("span:last-child");
e.preventDefault();
//lista.is(":hidden") ? $(this).find("ul").show() : $(this).find("ul").hide();
$(this).find("ul").toggle();
if(lista.is(":hidden")) {
triangulo.removeClass("triangulosup").addClass("trianguloinf");
}
else {
triangulo.removeClass("trianguloinf").addClass("triangulosup");
}
}
function clickli(e) {
var texto = $(this).text(),
seleccionado = $(this).parent().prev(),
lista = $(this).closest("ul"),
triangulo = $(this).parent().next();
e.preventDefault();
e.stopPropagation();
seleccionado.text(texto);
lista.hide();
triangulo.removeClass("triangulosup").addClass("trianguloinf");
}
$(".cajaselect").click(clickcaja);
$(".cajaselect").on("click", "li", clickli);
});