How to select all options from a select to submit?

Asked

Viewed 64 times

0

I am unable to send the selected options when I enter the code below. Out of code is working.

the page receives information and assembles the box according to the requests that came, so it is possible to withdraw orders and while doing this the screen needs to update to do some things on the server.

The problem is time to pass the remaining orders in the box to the server, in this piece of code I am not able to select the requests to send. In console.log it already informs that the value is null.

I’m referring to the last Else, everything else works.

Any idea?

'''

 $("#PedidoSelecionado").on("change",function(){
        $("#PedidoDisponivel").append($("#PedidoSelecionado option:selected").clone(true));
        $("#PedidoSelecionado option:selected").remove();
        ordenarOptions("PedidoDisponivel");
        ordenarOptions("PedidoSelecionado");
        <cfif variables.MensaErro neq "">
            if($("#PedidoSelecionado > option").length == 0){
                $("#PedidoSelecionado").addClass("is-invalid");
                $("#PedidoSelecionado").removeClass("is-valid");
                erro = true;
                mensagem = "<strong>Você precisa selecionar pelo menos um pedido.</strong><br>";
                $(".container-fluid .alert-danger").html(mensagem)>
                $(".container-fluid .alert-danger").show("slow",function(){
                    setTimeout(function(){
                        $(".container-fluid .alert-danger").hide("slow");
                    },
                    3000);
                });
            }
            else{
                $("#PedidoSelecionado option").each(function(){
                    $(this).attr("selected",true);
                });
                console.log($("#PedidoSelecionado").val());return;
                ChamaRotina('Estoque','Pedido',$("#PedidoSelecionado").val());
            }
        </cfif>
    });

'''

1 answer

0


Assuming that your select has the attribute multiple defined.

Instead of:

$("#PedidoSelecionado option").each(function(){
    $(this).attr("selected",true);
});

Use:

$('#PedidoSelecionado option').prop('selected', true);

About the .prop();

https://api.jquery.com/prop/

Heed:

For jQuery versions 1.6+ we have:

$('#PedidoSelecionado option').prop('selected', true);

And for old versions:

$('#PedidoSelecionado option').attr('selected', 'selected');

You can also use a function to assume this:

function selectAll(){
    $("#PedidoSelecionado").find("option").each(function(this) {
        $(this).attr('selected', 'selected');
    });
}
  • Oops @Eliseu B. Boy, I have this code you described on the page, but at this point it didn’t work. I managed to solve with the following code:

  • var options = $('#Requestselected option'); var values = $.map(options ,Function(option) { Return option.value; }); Chamarotina('Storing','Orders',values); "Thank you"

  • Okay, good that it was solved, when asking a question, make reference of versions you are using, if there are also dependencies and so can work an answer in the scenario of your doubt, facilitates, good luck.

Browser other questions tagged

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