Error javascript code and cannot find

Asked

Viewed 164 times

0

I don’t know much about javascript, so I did a web search and found a dynamical form code, only it shows this error http://prntscr.com/mp80qt, I have no idea what it might be.

$(function(){
  $("#estados").change(function(){
    var uf = $(this).val();
    $.ajax({
      type: "POST",
      url: "exibe_cidade.php?uf="+uf,
      dataType:"text",
      success: function(res){
        $("#cidades").children(".cidades").remove();
        $("#cidades").append(res);

      }
    });
  });
});

he interacts with this php code that was to pull a city list

<?php

require_once "func/conn.php";

$uf = $_GET['uf'];
$sql = 'SELECT * FROM tb_cidade WHERE uf = ? ORDER BY nome';
$stm = $conexao->prepare($sql);
$stm->bindValue(1, $uf);
$stm->execute();
$lista = $stm->fetchAll();
for($i=0;$i<count($lista);$i++){ 
    $id = $lista[$i]['id'];
    $nome = $lista[$i]['nome'];

    echo '<option value="'.$id.'" class="cidades">'.$nome.'</option>';
    }
?>
  • Use the jQuery https://www.w3schools.com/jquery/jquery_get_started.asp

1 answer

1


Missing add library jQuery, you can use this CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  • Vlw man, it helped me a lot, I stayed a long time breaking my head with this haha, thank you

  • If my answer solved your problem, you can accept by clicking on the side of the answer

  • I’m sorry to bother you again, but I wanted the code to remove 2 element at once, only it doesn’t want to work, I added an identical line with the top one so $("#cities"). Children(".cities"). remove(); $("#universities"). Children(".universities"). remove(); but n want to function, would know me to report what is missing?

  • Without HTML it’s complicated to answer, open a new question and add jQuery and HTML. Avoid asking too many questions in a single post

Browser other questions tagged

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