Javascript Running on JSFiddle but not on a webpage

后端 未结 6 2053
灰色年华
灰色年华 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 12:47

    You need a onload function so your code is run after your HTML is loaded. Try 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';
        };
    }
    

    You can also add the code after all your HTML, before the end of the body tag.

    And note that in your post you are missing tags.

提交回复
热议问题