Javascript Running on JSFiddle but not on a webpage

后端 未结 6 2127
灰色年华
灰色年华 2021-01-26 12:18

Ok so I have a code that would show different forms based on dropdown selection
Here\'s the fiddle to that..

Well its always giving me Test1 which means its not cha

6条回答
  •  逝去的感伤
    2021-01-26 13:06

    That is because in the fiddle your code is set to run at onLoad, but in your code its running before the DOM is created.

    Wrap your code into a window.onload event like this:

    window.onload = function()
    {
        document.getElementById('options').onchange = function() {
            var i = 1;
            var myDiv = document.getElementById(i);
            while(myDiv) {
                myDiv.style.display = 'none';
                myDiv = document.getElementById(++i);
            }
            document.getElementById(this.value).style.display = 'block';
        };
    };
    

    Anyway, like @positivew remembered, your code misses the tag. Is semantically correct to put your JS scripts inside it.

提交回复
热议问题