Javascript: If dropdownlist is selected, how to load the fields?

Asked

Viewed 462 times

-1

Boa Tarde Galera,

How do I load some fields if Dropdownlist value is selected?

  • my friend, post the code with your dropdownlist and what you want to carry.

  • I think it gives you a north. http://answall.com/questions/87411/selectr-o-pa%C3%Ads-e-trazer-o-estado/87417#87417

  • What I wanted was exactly what Erick Gallani sent, thank you all!

1 answer

2


It can be done like this

The html

<select id='meuDropdown'>
    <option value='1'>Opção 1</option>
    <option value='2'>Opção 2</option>
    <option value='3'>Opção 3</option>
    <option value='4'>Opção 4</option>
</select>

<input type='text' id='meuInputText' style='display: none' />

The javascript

<script type='text/javascript'>
        var dropdown = document.getElementById('meuDropdown');

        function onDropdownChanged()
        {
            console.log(dropdown.selectedIndex);
            //aqui você pode fazer o que quiser com o valor selecionado
            //por exemplo
            if(dropdown.options[dropdown.selectedIndex].value == 2) {
                //faz algo quando o valor selecionado for 2
                console.log('Selecionou o valor 2');

                //mostra o input quando o valor selecionado for 2
                var meuInput = document.getElementById('meuInputText');
                meuInput.style.display = 'block';
            }
        }

        if (dropdown.addEventListener)
        {
            dropdown.addEventListener('change', onDropdownChanged, false);
        }
        else
        {
            // suporte para o IE
            dropdown.attachEvent('onchange', onDropdownChanged, false);
        }
    </script>

Example in Jsfiddle https://jsfiddle.net/5a0bqo8o/2/

I hope I’ve helped

Browser other questions tagged

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