How to select a dropdown loaded dynamically with jQuery?

0

I'm using a form to record dynamic questions in my system. I'm also recording this data in a session so you do not have to be selecting all the time in dropdows but you have a problem:

In submit , in the Select a category: field, I get the category ID in the session and auto-select it with PHP and give it a jQuery trigger so that through of the Category ID, it queries the database and populates the Select an activity dropdown: ...

  

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

At load, the system takes the selected ID in Category and queries 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));
            });

        }

    });

});

    
asked by anonymous 15.10.2016 / 14:54

1 answer

1

On the PHP side when you make the request to get the activity list, you can check to see what activity is in your session, add a parameter to that list, for example current = true, and when the your request arrives from the javascript side you can check if current is true and add the "selected" attribute.

    
17.10.2016 / 19:55