Pick select selected (only picking first value)

Asked

Viewed 18,567 times

7

An Alert is displayed (with the sweetalert plugin) and in that Alert has a selectwith names. After selecting the name, the person has to click the button Add, when the person clicks the function of the code below is executed and in it I put a code to capture the option selected, only that 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");
                                }
                            }
                        });

                    }

                });
            })

2 answers

7

There are some ways to get the value, as there is your HTML I will quote some:

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

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

You can catch it straight, like this:

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

jsfiddle

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

jsfiddle

If you own a value and want to pick up the selected text can do so:

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

jsfiddle

In your example above, it seems to be missing only pick up the text with the method .text().

  • the worst is that it didn’t work. When I click on "Add" it gives Alert only that the result only comes out 1 (which is the value of the first item). I do not know if there is something, but this "Add" is a button of the plugin that only makes the function of the code I posted.

  • better look at that jsfiddle hehe'

  • I don’t understand why o.o.. Because in mine it looks like the code of the jsdiddle ... The only difference is that the <select> is rendered in a <div> with the display:hidden and when the person clicks on a link jQuery takes the HTML of that div and plays in the plugin to display select with options

  • Just to clarify, your select stays within the modal generated by sweetalert ? :D

  • 1

    Yes, I managed to.. It was because how I picked up the rendered content of <div> who was with display:none and then just create another one and put inside the modal, so was 2 select with the same name and gave dick... I managed to fix.. Thank you!

  • 1

    @Alissonacioli forgot to upvote and close that ;) ... (then delete this comment...)

Show 1 more comment

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
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.