Problem to run 'change' event in select

0

I have the following SELECT:

<select id="municipio" class="celda corto">
    <option selected="true" disabled="disabled"><?= $rows['municipio'] ?></option>
    <option>Veracruz</option>
    <option>Boca del Rio</option>
</select>

What I am looking for is that it appears by default (it is visible before selecting anything) the option that is obtained from the database, but with this line:

<option selected="true" disabled="disabled"><?= $rows['municipio'] ?></option>

My event stops working:

$('#municipio').on('change', function() {
    alert('Prueba');
});

How can I solve it?

    
asked by Oscar Díaz 07.06.2017 в 20:00
source

3 answers

0

Responding to your comment, if the query returns empty fields you should filter them with a where, it would be fine if you put the query.

SELECT CAMPO_ID, TRIM(CAMPO_NOMBRE) FROM TABLA WHERE CAMPO_NOMBRE <> ""

In your script, try it as follows:

$(document).ready(function(){
$(document).on('change','#municipio',function(){
  alert('Prueba');
  });
})
    
answered by 07.06.2017 / 22:18
source
0

What you should do is give the corresponding value

<select id="municipio" class="celda corto">
    <option selected="true" value="<?php echo $rows['municipio'] ?>" disabled="disabled"><?php echo $rows['municipio'] ?></option>
    <option value="Veracruz">Veracruz</option>
    <option value="Boca del Rio">Boca del Rio</option>
</select>

And then you do the next test

$('#municipio').on('change', function() {
    var municipio = $(this).val();
    alert(municipio );
});

You have the test and I hope it works for you.

    
answered by 08.06.2017 в 17:26
0

You should upload it like this:

<select id="municipio" class="celda corto">
    <? if ($rows['municipio'] != "") { ?>
        <option value="<?php echo $rows['municipio'];?>'" selected="true"><?= $rows['municipio'] ?></option>
    <? } ?>
    <option value="Veracruz">Veracruz</option>
    <option value="Boca del Rio">Boca del Rio</option>
</select>

If the value of the database comes, add the <option> selected and the value. If not, add the other options to the selector and also, you still needed to add the attribute value in the <options> for when when you use the value po you can rescue.

    
answered by 08.06.2017 в 17:33