Combo <Select> does not work

Asked

Viewed 122 times

0

I need the second <select> receive the data according to the choice of the first. The <option> in the first <select>, but in the second it doesn’t move.

I have a folder textdata with all .txt concerning the <option> of each correspondence.

HTML

<select id="first-choice">
  <option selected value="0">>>> Selecione</option>
  <option value="AC">AC</option>
  <option value="AL">AL</option>
  <option value="AM">AM</option>
  <option value="AP">AP</option>
  <option value="BA">BA</option>
  <option value="CE">CE</option>
  <option value="DF">DF</option>
  <option value="ES">ES</option>
  <option value="GO">GO</option>
  <option value="MA">MA</option>
  <option value="MG">MG</option>
  <option value="MS">MS</option>
  <option value="MT">MT</option>
  <option value="PA">PA</option>
  <option value="PB">PB</option>
  <option value="PE">PE</option>
  <option value="PI">PI</option>
  <option value="PR">PR</option>
  <option value="RJ">RJ</option>
  <option value="RN">RN</option>
  <option value="RO">RO</option>
  <option value="RR">RR</option>
  <option value="RS">RS</option>
  <option value="SC">SC</option>
  <option value="SE">SE</option>
  <option value="SP">SP</option>
  <option value="TO">TO</option>
  <option value="WW">WW</option>
</select>
<br>
<select id="second-choice">
  <option>Selecionar UF</option>
</select>

JAVASCRIPT

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script type="text/JavaScript">
$("#first-choice").change(function() {
   $("#second-choice").load("textdata/" + $(this).val() + ".txt");
});
</script>

A part of one of the archives .txt

<option>São Paulo - SP001</option>
<option>Adamantina - SP002</option>
<option>Adolfo - SP003</option>
<option>Aguaí - SP004</option>
<option>Águas da Prata - SP005</option>
  • Pq is loading 2 different jquerys at the same time?

  • Try to place the second parameter of the .load() to give an Alert stating whether the shipment was successfully done or not, it helps a lot to discover.

  • The values of the 1st select are uppercase, so the names of . txt tb must be uppercase (e.g. AC.txt, SP.txt etc...) otherwise you will not find. Or you can convert to lower case values: $(this).val().toLowerCase()

  • @Sam doesn’t have a logical explanation for this. All txt are capitalized.

1 answer

1


Hello, at first your code is correct. What can be happening is that your files . txt are not inside an HTTP server and so jQuery does not load them. Install the extension Web Server in Chrome and configures it to serve the files in your folders. My folder structure looks like this:

C:\stackoverflow\exemplo01
C:\stackoverflow\exemplo01\index.html
C:\stackoverflow\exemplo01\textdata\AC.txt

I configured the Web Server to serve the *C: stackoverflow folder files*.

Hence, only you access in the browser your folder using the Web Server URL:

http://127.0.0.1:8887/exemplo01/

Your code is correct:

inserir a descrição da imagem aqui

  • I was testing for Dreamweaver, the file teste1.php on the desktop and next to the folder textdata capitalized txt. Nothing works, even if I put it on the server, it doesn’t work http://viacircular.com.br/cesar/2018/teste1.php

  • The problem that is occurring is that how you put your js to load at render time GIFT, when you use the selector #first-Choice, it is not yet loaded to bind the event change. Solution: $(document).ready(function(){&#xA; $("#first-choice").change(function() {&#xA; $("#second-choice").load("textdata/" + $(this).val() + ".txt");&#xA; });&#xA; }); Put your js Imports always at the end of all your HTML before closing the body tag.

  • I did so but still nothing http://viacircular.com.br/cesar/2018/teste2.php

Browser other questions tagged

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