How to recover ID in JQUERY

Asked

Viewed 42 times

-1

The goal is popular a select according to the previous select.

Script

<script>
        $(function () {
            $('#adms_grps_prod').change(function () {
                if ($(this).val()) {
                    $('#adms_tps_prod').hide();
                    //console.log('tipo ocultado');
                    $.get('../outros/tps_prods?search=', {adms_grps_prod: $(this).val(), ajax: 'false'}, function (j) {
                        var options = '<option value="">Escolha o Tipo</option>';
                        //console.log(options);
                        for (var i = 0; i < j.length; i++) {
                            //options += '<option value="' + j[i].id + '">' + j[i].nome + '</option>';
                            //alert(j[i]);
                            console.log("Script: " + j);
                        }
                        $('#adms_tps_prod').html(options).show();
                        $('.carregando').hide();
                    });
                } else {
                    console.log('Ocorreu um erro!');
                }
            });
        });
    </script>

With the above script, I got the following result:

<!DOCTYPE html>
<html lang="pt-br">
    <pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp64\www\site_delivery\adm\app\cpadms\outros\tps_prods.php:33:</small><small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
</pre>ID: 1[{"id":"2","nome":"Cerveja Devassa 600ml"},{"id":"1","nome":"Cerveja Heineken 600ml"}]</html>

How do I get only the ID?

1 answer

0

I tried this to get the id value, currently selected.

$('#adms_grps_prod option:selected').attr('id')
  • Had already done this way, but presented "Undefined".

Browser other questions tagged

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