我最近刚开始学JS,看到cookie这一章的时候,有一个例子怎么也出不来,请高手指教难道onsubmit里面location是不起作用的?
我在location前面加了一句alert(document.cookie);是可以输出的,但是页面不会进行跳转
直接写成onsubmit("window.location.href='12-1.html';")也不会进行跳转
把window去掉也不行,改成self也不行,甚至改成document也不行,href去掉也不行
后面的地址改成绝对位置也不行
莫非是我拼写错误?部分代码如下:<html>
<head>
<title></title>
<style type="text/css">
<!--
.textStyle
{
width:150px;
height:20px;
}
-->
</style>
<script type="text/javascript">
<!--
function setCookie(name,value,expires,path,domain,secure)
{
var curCookie=name+'='+encodeURI(value)+(expires?(';expires='+expires.toUTCString):'')+(path?(';path='+path):'')+(domain?';domain='+domain:'')+(secure?';secure':'');
document.cookie=curCookie;
}
function loginCheck()
{
if (document.form1.username.value=='www' && document.form1.password.value=='1234')
{
var login=true;
var now=new Date();
now.setDate=(now.getDate()+30);
setCookie('login',login,now);
document.location.href='12-1.html';//这句话不起作用
}
}
-->
</script>
</head>
<body>
<form name="form1" action="" method="post" onsubmit="loginCheck();">
<p>
用户名:<input type="text" name="username" class="textStyle" />
</p>
<p>
密&nbsp;&nbsp;码:<input type="password" name="password" class="textStyle" />
</p>
<p>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</p>
</form>
</body>
</html>

解决方案 »

  1.   

    window.location='12-1.html';//这句话不起作用
    试试这样
      

  2.   

    改成这样,
    <html>
    <head>
    <title></title>
    <style type="text/css">
    <!--
    .textStyle
    {
    width:150px;
    height:20px;
    }
    -->
    </style>
    <script type="text/javascript">
    <!--
    function setCookie(name,value,expires,path,domain,secure)
    {
    var curCookie=name+'='+encodeURI(value)+(expires? (';expires='+expires.toUTCString):'')+(path?(';path='+path):'')+ (domain?';domain='+domain:'')+(secure?';secure':'');
    document.cookie=curCookie;
    }
    function loginCheck()
    {
    if (document.form1.username.value=='www' && document.form1.password.value=='1234')
    {
    var login=true;
    var now=new Date();
    now.setDate=(now.getDate()+30);
    setCookie('login',login,now);
    window.location.href='12-1.html';//这句话不起作用
    }
    }
    -->
    </script>
    </head>
    <body>
    <form name="form1" action="" method="post" >
    <p>用户名:<input type="text" name="username" class="textStyle" /></p>
    <p>密&nbsp;&nbsp;码:<input type="password" name="password"  class="textStyle" /></p>
    <input type="button" value="提交" onclick="loginCheck()" />
    <input type="reset" value="重置" />
    </form>
    </body>
    </html>
      

  3.   

    TO mykelly6:这个我已经试过,前面也写了TO Free_Wind22:这个方法确实可行,但是其实,我是想知道是不是onsubmit里面location是不起作用的。如果硬要改也还有很多方法,我现在主要是为了学习,而不是程序的运行成功,不过还是感谢解答TO zzqkillyou:我在前面也写了“我在location前面加了一句alert(document.cookie);是可以输出的,但是页面不会进行跳转”。我想前面应该没有问题
      

  4.   

    location不是document的对象吧,location是window的对象,与document地位是平等的
      

  5.   

    我知道错误在哪里了
    输入错误ORZ
    now.setDate=(now.getDate()+30);多打了个等号= =
    很奇怪,location无法跳转,但是alert和document.write却能输出