Ways to create combobox using AJAX to fill

0

I currently have a regular HTML combobox:

<select name="nomeCombox1" id="nomeCombox1" class="select"> 
    <option value="0" selected="selected">Selecione box1</option>
    <?php
    $res = odbc_exec($conexao, "Select DISTINCT desc from tab1");
    while ($row = odbc_fetch_array($res)) {
        echo "<option name='nomeCombox1' value='" . $row['desc'] . "'>" . $row['desc'] . "</option>";
    }
    ?>
</select>

This combo is correctly fetching the data.

In the next step, I have a JS where I look for what was selected and play in a new combobox:

$("#nomeCombox1").change(function () {
    var idCombox1 = $(this).val();
    $.ajax({
        dataType: 'json',
        url: "js/ajaxBuscar.php",
        data: {idCombox1: idCombox1},
        success: function(data) {
            var q = '<select name="idCombox2" id="idCombox2" class="select"> <option value="0">Escolha</option>';
            for(i=0; i < data.length; i++ ){
                q += '<option value=' + data[i].idCombox2 + '>' + data[i].descricao + '</option>';
            }
            q += '</select>';
            $('#div1').html(q);
        }, error: function(request, status, error) {
            alert(request.responseText);
        }
    });
});

These codes are all 100% working. My main question would be: How can I create this Combobox2 without being via JS? That is, without having to play $('#div1').html(q) . Is there another way?

I'm wanting another way because I have a function in JS where I would have to check this value of that second combobox and fill an input with the result of another AJAX. When I put combobox2 straight into HTML it works. What I'm guessing is that the moment the page loads, there is no such second combo, so JS can not execute correctly.

    
asked by anonymous 28.10.2014 / 14:51

1 answer

1

I'm not sure if this is your case, but I've had some similar problems in the past.

When the DOM is formed if its element has not been loaded, Jquery events will not be recognized.

For example, let's look at the following code snippet:

$('.classe').click(do());

28.10.2014 / 17:01