How to redirect the user (to another page) after your choice?

Asked

Viewed 1,280 times

1

I’m having difficulty making the code below work, I need when selecting a city, this was redirected to a particular page.

Below follows the code I’m trying to use:

Javascript:

<script type="text/javascript" src="http://pastebin.com/raw.php?i=Qi2BcFsP"></script> 
<script type="text/javascript">
window.onload = function() {

  new dgCidadesEstados({
    estado: document.getElementById('estado'),
    cidade: document.getElementById('cidade'),
     estadoVal: '<%=Request("estado") %>',
     cidadeVal: '<%=Request("cidade") %>'
  });
}
</script>

HTML:

</head>
<body>    
<form id="sistema" name="sistema" method="post" action="">
<label>Estado</label>
    <select id="estado" name="estado"></select>
    <label>Cidade</label>
    <select id="cidade" name="cidade"></select>
</form>
</body>
  • Alex in my answer in the other question did not help? You saw that I edit it?

  • I had great difficulty. And with this code I was able to easily insert or remove cities and states. I don’t know where I’m going wrong.

  • There are two codes in the answer is the last, I will post here.

  • With this code there I did everything and it worked out.. I just need to redirect now is.

  • the code there in the answer does just that, writes, I can’t understand, you didn’t see the update? All right, follow the answer with the example of redirect copied from another answer. To test the code in the answers click on "Execute code snippet"

2 answers

2


Add an event change in his select of the city, then you can know what the value.

<script type="text/javascript" src="http://pastebin.com/raw.php?i=Qi2BcFsP"></script> 

<form id="sistema" name="sistema" method="post" action="">
<label>Estado</label>
    <select id="estado" name="estado"></select>
    <label>Cidade</label>
    <select id="cidade" name="cidade"></select>
</form>

<script type="text/javascript">
    window.onload = function() {

      new dgCidadesEstados({
        estado: document.getElementById('estado'),
        cidade: document.getElementById('cidade'),
         estadoVal: '<%=Request("estado") %>',
         cidadeVal: '<%=Request("cidade") %>'
      });
  
      document.getElementById('cidade').addEventListener("change", function(){
        var cidadeSelecionada = this.value;
    
        if (cidadeSelecionada && cidadeSelecionada != "Selecione uma cidade") {
           //document.location.href = "suaurl.com?cidade=" + cidadeSelecionada;
           alert(cidadeSelecionada);
        }
      });
    }
</script>

  • Maicon could give me an example with two cities opening pages of different sites ?

  • @ALEXBROOK Different sites as? If more specific I can make you the example

  • For example: after selecting the city of Divinópolis the page http://redeglobo.com.br/. After selecting the city of Belo Horizonte opens the page: http://google.com

  • There is the possibility of changing the way the select?

  • Can I send you the file so I can analyze it ? But if you access this link http://pastebin.com/raw.php?i=Qi2BcFsP you can see how this file structure.

  • @ALEXBROOK Take a look at this example here: http://jsfiddle.net/h890j0r9/1/

  • Basically I add the url to value option and then just redirect

  • put in the script and did not work ? could insert in the script.

  • as you put?

  • 1

    Are you really trying to understand the code or are you just copying and pasting?

  • To understand you have to start from the "beginning" and not from the ready solutions

  • I see your difficulty is in language, no point in me telling you how to do

  • https://www.youtube.com/watch?v=t4Y7jd4h-T8

Show 8 more comments

1

Like I posted on another answer the example of redirecting and explained:

Use the event change, as in the example the beginning of the code should look like this:

window.onload = function() {
    var estados = document.getElementById('estado');
    var cidades = document.getElementById('cidade');

    cidades.onchange = function()
    {
        if (cidades.value !== "") {
            //Basta modificar está linha conforme a necessidade
            window.location = "pagina.asp?estado=" + estados.value + "&cidade=" + cidades.value
        }
    };

    new dgCidadesEstados({
        estado: estados,
        cidade: cidades,
        estadoVal: '<%=Request("estado") %>',
        cidadeVal: '<%=Request("cidade") %>'
    });

    var opts = estados.getElementsByTagName("option");
    var i = 0, j = opts.length, e, remove = [];

    for (; i < j; i++) {
        e = opts[i];
        if (e.value !== "" && e.value !== "MG" && e.value !== "SP") {
            //Pega o elemento que será removido e adiciona ao vetor/array
            remove.push(e);
        }
    }

    i = 0;
    j = remove.length;

    for (; i < j; i++) {
        //Remove todos que são diferentes de Minas Gerais, São Paulo e vazio (este ultimo equivale ao "Selecione um estado")
        estados.removeChild(remove[i]);
    }
};
<script src="https://cidades-estados-js.googlecode.com/files/cidades-estados-1.2-utf8.js"></script>

<form id="sistema" name="sistema" method="post" action="">
<label for="cidade">Estado</label>
<select id="estado" name="estado"></select>

<label for="cidade">Cidade</label>
<select id="cidade" name="cidade"></select>
</form>

To change the destination address just change the line you are on window.location =

Browser other questions tagged

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