0

I have a form with a select input. I want to execute a jQuery function when the value of the select input changes:

This is the select input:

<div class="form-group required">
        <label class="col-sm-3  control-label">Marca del vehículo</label>
        <div class="col-sm-9">
            <select class="form-control" name="marca" id="marca">
                <option value="">Selecciona una marca de vehículo</option>
                <?php
                $sql = "SELECT * FROM car_make ORDER BY name ";
                $res = $mysqli->query($sql);
                while($row = $res->fetch_assoc())
                    {
                    echo"<option value=´{$row["id_car_make"]}']>{$row["name"]} 
                    </option>";
                    }


               ?>



            </select>
      </div>
</div> 

And this is the script that should launch the function when the select value changes:

<script>


        if (window.jQuery) {  
            // jQuery is loaded  
            alert("Yeah!");
        } else {
            // jQuery is not loaded
            alert("Doesn't Work");
        }


 $("#marca").change(function(){

    cid = $(this).val();
    $.post("modelo.php",{id:cid}, function(data){
            $("#modelo").html(data);
        });

    });

I have included two alerts to confirm that jQuery is loaded.

Now, after changing the select value, the script is not launched.

I am not able to find the reason why the function is not launched

mvasco
  • 4,965
  • 7
  • 59
  • 120
  • 1
    Wrap your jQuery code in a document.ready event handler: http://learn.jquery.com/using-jquery-core/document-ready/ – Rory McCrossan Jun 07 '18 at 14:52
  • Are you sure that the function isn't getting run, or is it simply not doing what you think? put a breakpoint in side the function to make sure – James Parsons Jun 07 '18 at 14:52
  • 1
    [Don't use `alert()` for debugging](https://stackoverflow.com/questions/8203473/why-is-console-log-considered-better-than-alert). [Open the browser's console](https://webmasters.stackexchange.com/questions/8525/how-do-i-open-the-javascript-console-in-different-browsers), look for error messages. – JJJ Jun 07 '18 at 14:52
  • 1
    @RoryMcCrossan, thank you, it works. I took this script from a tutorial and there it worked. – mvasco Jun 07 '18 at 14:57

0 Answers0