How to select a dynamically charged dropdown with jQuery?

Asked

Viewed 190 times

0

I am using a form to register dynamic questions in my system. I’m also recording this data in a session so I don’t have to be selecting all the time dropdows but there’s a problem:

In the Submit, in the field Select a category: i take the category ID in the session and auto-select with PHP and give a Trigger with jQuery so that, through the category ID, it consult the database and fill the field dropdown Select an activity from: ...

Now comes the bullshit: I want to leave selected too Activity because are several questions for the same Category and Activity.

When loading, the system picks up the ID selected in Category and consults on Activity. What I have so far are two ID’s in a Session (PHP) and the jQuery code below:

$(document).ready(function() {

    var param = $("#categoriaCad option:selected").val();

    $.ajax({
        url: CAMINHO_JAVASCRIPTI+"/usuarios/ajaxsubcat",
        type: "POST",
        dataType: "json",
        data: {param: param},
        success: function(data){

            $('#cadSubcategoria').find('option').remove().end().append('<option value="">Selecione uma categoria ...</option>');

            $.each(data, function(chave, valor) {

                $('#cadSubcategoria').append($('<option>').attr('value', valor.id).text(valor.atividade));
            });

        }

    });

});

inserir a descrição da imagem aqui

1 answer

1

Next to PHP when you make the request to get the list of activities, you can do the check to know what activity is in your session, add a parameter to that list, for example current = true and when your request arrives on the javascript side you can check if current is true and add the attribute "Selected".

Browser other questions tagged

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