Ways to create combobox using AJAX to fill

Asked

Viewed 1,867 times

0

Currently I have a normal 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 searching the data.

In the next step, I have a JS where I search 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 working all 100%. My main question would be: How can I create this Combobox2 without being via JS? IE without having to play $('#div1').html(q). There is another way?

I am wanting another way because I have a Function in JS where I would have to check this value of this second combobox and fill in an input with the result of another AJAX. When I put the combobox2 right into HTML it works. What I’m thinking is that at the moment the page loads does not exist this second combo and so the JS can not perform correctly.

  • Hi, Felipe, welcome to [en.so]. The title of the question does not match much with "How can I create a Combobox without being via JS?"... What do you mean, no JS? If you are already using jQuery/AJAX...

  • These codes are working all 100%. 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 this second combobox and fill in an input with the result of another AJAX. When I put the combobox2 right into HTML it works. What I’m thinking is that at the moment the page loads does not exist this second combo and so the JS can not perform correctly.

  • But that which I explained, is already in the question!?!

1 answer

1


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

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

To illustrate let’s see the following code snippet:

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

If any element was added dynamically with class="class" the bind would not work because when the element was added the bind for it was not realized, it was only done for the elements already loaded.

The solution I found to solve this problem was the following:

$(document).on('evento', 'seletor', action());

So each time an event is triggered the elements satisfying the selector are checked regardless of whether it has already been loaded into the DOM initially or not.

For more information: https://api.jquery.com/on/

I hope I’ve helped!

  • Perfect Richard. Solved my problem! I didn’t know this kind of "shot"! My code looks like this: $(Document). on('change', 'select[name=idCombox2]', Function(){

  • I’m glad it was helpful! I know I suffered a little before discovering this.

Browser other questions tagged

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