Show the selected text dropdown

2

I have a dropdown that I filled with AJAX dynamically and it shows correctly, I need to show the selected one, I tried this without results:

 alert($('#ul_sucursal').find('option:selected').text());
 alert($( "#ul_sucursal option:selected" ).text());



<ul id="ul_sucursal" class="dropdown-menu" role="menu">
    <!-- lo lleno con ajax-->                   
</ul>

$.ajax({
            type: "POST",
            url: "<?= base_url() ?>DashboardOTController/ListarSucursales",
            success: function(obj){



               $.each(obj.Sucursales.sucursales, function (ind, elem) {//inserta en el ul li de sucursal

                $('#ul_sucursal').append('<li><a href="#"  value="'+elem.id_sucursal+'">'+elem.nombre_sucursal+'</a></li>');


              });
          }
    });
    
asked by Javier Antonio Aguayo Aguilar 27.06.2017 в 18:16
source

2 answers

1

If you only want to show the value of what you selected, use the onclick of the anchor tag:

$('#ul_sucursal').append('<li><a href="#" onclick="alert('+elem.id_sucursal+')" value="'+elem.id_sucursal+'">'+elem.nombre_sucursal+'</a></li>');
    
answered by 27.06.2017 / 18:21
source
1

As the data is added dynamically with AJAX the event must be captured as follows:

$("ul_sucursal").on("click", "a",function (e) {
e.preventDefault()
//para que no nos lleve el link a algun lugar
var valor = $(this).attr("value");

alert("este es mi valor" + valor)
});
    
answered by 27.06.2017 в 18:42