1

我在使用 javascript 重定向页面时遇到问题。我想在单击按钮后重定向页面,但它会自动重定向。这是为什么?

window.onload=initAll; 
function initAll(){
   if(window.addEventListener) {
      document.getElementById('check').addEventListener("click",redirection,false);
   } else if (window.attachEvent){ 
      document.getElementById('check').attachEvent("onclick", redirection);
   }
   function redirection(){
      window.location.href='http://www.google.com';
   }
}


<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <script type="text/javascript" src="glavna.js"></script>
   </head>
   <body>
      <div id="wrapper">
         <form action="glavna.html">City:<input id="city" type="text"/>&nbsp;&nbsp;
            Price: From:<input type="text" id="from"/>&nbsp; To:<input type="text" id="to"/>
            &nbsp;&nbsp;Refresh in min :<input type="text" id="refresh"/>
            <button id="check" >Search</button>
         </form>
      </div>
   </body>
</html>
4

2 回答 2

3

这可以通过window.location.href它的功能来完成

// similar behavior as clicking on a link
window.location.href = "you want to redirect";
于 2013-01-25T17:58:16.037 回答
0

window.location是位置对象。window.location.href改用重定向

于 2012-05-06T10:04:03.080 回答