Goodsu

导航

 

最近在使用 window的location时碰到一个无法跳转页面的问题,

后来在location语句后加了一条这样的语句:window.event.returnValue = false;
然后竟然可以正常跳转了,具体原因还在学习中,稍后更新。

<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<form action="" method="get">
    <label for="inp" id="label">
        tel:<input id="inp" type="text" placeholder="enter your tel">
    </label><br/>
    <button id="btn1" onclick="jData()">submit</button>
</form>

<script type="text/javascript">
    var input=document.getElementById("inp");
   function jData(){
       var pattr=new RegExp(/\d{11}/);
       var res=pattr.test(input.value);
       if(res){
           window.location.assign("http://www.baidu.com");
           window.event.returnValue = false;
       }
   }
</script>
</body>
</html>

 

后来发现,这个onlick是在form表单里。莫非这里页面跳转与form的action冲突?

后来试了去掉form标签,不用添加window.event.returnValue = false也可以正常跳转。

至于为什么form表单影响到了location,以后学习了再分析。

以下是没有使用form的code,chrome/firfox测试了没问题

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
<label for="inp" id="label">
    tel:<input id="inp" type="text" placeholder="enter your tel">
</label>
<span id="span"></span>
<br/>
<button id="btn1" onclick="jData()">submit</button>

<script type="text/javascript">
    var input=document.getElementById("inp");
    var label=document.getElementById("label");
    var span=document.getElementById("span");
    function jData(){
        var pattr=new RegExp(/\d{11}/);
        var res=pattr.test(input.value);
        if(res){
//           window.location.href="http://www.baidu.com";
            window.location.assign("http://www.baidu.com");
//           window.event.returnValue = false;
        }else{
            span.innerHTML="the tel num wrong,pls enter again";
            span.style.color="red";
        }
    }
</script>
</body>
</html>

 

posted on 2015-08-07 09:40  Goodsu  阅读(1193)  评论(0编辑  收藏  举报