未捕获的TypeError:无法读取属性' addEventListener' null下拉菜单

时间:2016-06-23 16:56:40

标签: javascript html

为什么我收到此错误?这应该工作,我试图做的是当有人在下拉菜单中选择表单并切换链接时,我想要在选择框上最后选择的内容保留在下拉菜单中但它保持一路回到顶端。感谢

我一直得到的错误是

未捕获的TypeError:无法读取属性' addEventListener'为null



    <!doctype html>  
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>MyWebsite</title>
        <link rel="stylesheet" href="languageswitcher.css">

      </head>

      <body>

        <header>

          <div id="country-select">
            <form action="" method = "get">
               <select id="locale" name="locale">
					<option value="en_US" title = '0'>English(US)</option>
					<option value="en_GB" title = '1'>English(UK)</option>
					<option value="bg_BG" title = '2'>Bulgarian</option>
					<option value="cs_CS" title = '3'>Czech</option>
					<option value="da_DK" title = '4'>Danish</option>
					<option value="de_DE" title = '5'>German</option>
					<option value="ek_GR" title = '6'>Greek</option>
					<option value="es_ES" title = '7'>Spanish</option>
					<option value="et_ET" title = '8'>Estonian</option>
					<option value="fi_FI" title = '9'>Finnish</option>
					<option value="fr_FR" title = '10'>French</option>
					<option value="hu_HU" title = '11'>Hungarian</option>
					<option value="it_IT" title = '12'>Italian</option>
					<option value="lt_LT" title = '13'>Lithuanian</option>
					<option value="lv_LV" title = '14'>Latvian</option>
					<option value="nl_NL" title = '15'>Dutch</option>
					<option value="no_NO" title = '16'>Norwegian</option>
					<option value="pl_PL" title = '17'>Polish</option>
					<option value="pt_PT" title = '18'>Portugese</option>
					<option value="ro_RO" title = '19'>Romanian</option>
					<option value="sk_SK" title = '20'>Slovak</option>
					<option value="sl_SL" title = '21'>Slovenian</option>
					<option value="sv_SE" title = '22'>Swedish</option>
              </select>
              <input value="Select" type="submit"/>
            </form>
          </div>
        </header>
                   <script>
	        // creates the page dynamically
			function GetSelectedItem(){
				var option = document.getElementById("locale").value;
			}
	

			document.getElementById("locale").addEventListener('change', function(event) {
				var selected = event.target.options[ event.target.selectedIndex ].value 
				console.log(selected);
				window.location.hash = selected; 
			});
        </script>
      </body>
    </html>
&#13;
&#13;
&#13;

0 个答案:

没有答案