Get select selected (only getting first value)

7

An alert is displayed (with the sweetalert plugin) and in this alert has a select with names. After selecting the name, the person has to click the Add button, when the person clicks the function of the code below is executed and in it I put a code to capture the selected option , only the result is always that of the first option .

$("a#AddListaPresentes").click(function(){

                var conteudoListaPresente = $("#contentLP").html();
                var id_produto = $(this).attr('ref');
                var baseURL = $("#baseURL").val();

                swal({
                  title: "Adicionar a Lista de Presentes",
                  text: conteudoListaPresente,
                  html: true,
                  showCancelButton: true,
                  closeOnConfirm: false,
                  closeOnCancel: true,
                  cancelButtonText: 'Cancelar',
                  confirmButtonText: 'Adicionar'
                },function(){

                    var selecionado = $("#ListaPresenteSelect option:selected");

                    alert(selecionado);

                    if($.trim(selecionado) !=  false){

                        $.ajax({

                            url: baseURL+'ajax/adicionaritemlistadepresentesx',
                            type: 'POST',
                            data:{id:selecionado, produto:id_produto},

                            success:function(callback){

                                if(callback){
                                    swal("Adicionado!", "Produto adicionado a sua lista de casamento (:", "success");
                                }else{
                                    swal("Opss", "Ocorreu um erro ao adicionar o produto: "+callback, "error");
                                }
                            }
                        });

                    }

                });
            })
    
asked by anonymous 01.11.2015 / 02:29

2 answers

7

There are a few ways to get value, as you do not have your HTML I'll name a few:

If your form does not have the value attribute, something like:

<select id="ListaPresenteSelect">
    <option>Option1</option>
    <option>Option2</option>
    <option>Option3</option>
</select>

You can take it straight, like this:

$('#ListaPresenteSelect').val();

jsfiddle

If you have a value attribute and you want to access it, just use the example described above.

jsfiddle

If you have a value and you want to get the selected text you can do this:

$('#ListaPresenteSelect option:selected').text();

jsfiddle

In your example above, it seems to be missing just getting the text with the method .text() .

    
01.11.2015 / 03:11
0

This will select what is clicked

$(document).ready(function () {

    // get estado

    //variaveis{
    var estado = $('select');

    //variaveis}  

    estado.change(function () {
        console.log(estado.val());
    });
    //final get estado
});
    
18.03.2017 / 17:24