Place a url inside option select

-1

How to put a url inside a select's option? When the user selects the select option it would already redirect to url within the option.

The way I tried the url is concatenated with the previous one and the link is broken.

    
asked by anonymous 11.10.2016 / 16:01

2 answers

0

Do as follows:

<select id="link">
   <option value="" selected>Escolha o site</option>
   <option value="http://www.google.com">Google</option>
   <option value="http://www.youtube.com">YouTube</option>
   <option value="http://pt.stackoverflow.com/">Stack</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
 $(document).ready(function(){

    $('#link').on('change', function () {
         var url = $(this).val(); 
         if (url) { 
             window.open(url, '_blank');
          }
          return false;
        });
     });
</script>
    
11.10.2016 / 18:26
0

To place the link within a single option do as follows:

<script type="text/javascript">
function changeFunc($i) {
    if ($i == 'nova'){
        window.open('index.php?pg=cadastrar_categoria','_self');
    }
}
</script>

<select id="link"  onchange="changeFunc(value);">
   <option value="" >Escolha a categoria</option>
   <option value="animais">Animais</option>
   <option value="natureza">Natureza</option>
   <option value="nova">Adicionar nova categoria</option>
</select>
    
05.01.2019 / 18:08