日期:2014-05-16  浏览次数:20412 次

IE报错,id未定义
JScript code

<script>
function SetCookie(name,value) //储存cookie函数
{
    var Days = 30; 
    var exp  = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)  //读取函数
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;

}
function setCS(obj) { SetCookie ("sl", obj.selectedIndex); }//OPTION 改变的时候 储存选择的是哪一项 
</script>
<script>
document.getElementById(mySelect).options[getCookie('sl')].selected=true; //读取上次选择的选项
</script>
<div class="search-filter">
  <select name='typeid' id="mySelect" onChange="setCS(this)"  class="nav">
    <option value='65'> 手机</option>
    <option value='92'> 微博</option>
    <option value='93'> 社交网</option>
    <option value='94'> 电子商务</option>
    <option value='95'> 浏览器</option>
    <option value='96'> 百度</option>
  </select>
</div>



在IE 中打开页面报错:mySelect未定义。
求大哥指点迷津!

------解决方案--------------------
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

</head>

<body>
    <script>
    function SetCookie(name,value) //储存cookie函数
    {
        var Days = 30; 
        var exp  = new Date();
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }
    function getCookie(name)  //读取函数
    {
        var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
        if(arr != null) return unescape(arr[2]); return null;
    
    }
    function setCS(obj) { SetCookie ("sl", obj.selectedIndex); }//OPTION 改变的时候 储存选择的是哪一项 
    </script>
    
    <div class="search-filter">
      <select name='typeid' id="mySelect" onChange="setCS(this)"  class="nav">
        <option value='65'> 手机</option>
        <option value='92'> 微博</option>
        <option value='93'> 社交网</option>
        <option value='94'> 电子商务</option>
        <option value='95'> 浏览器</option>
        <option value='96'> 百度</option>
      </select>
    </div>
    <script>
    if(getCookie('sl'))
        document.getElementById('mySelect').options[getCookie('sl')].selected=true; //读取上次选择的选项
    </script>
</body>
</html>