2015-06-25 47 views
1

点击提交按钮后,我想导航到不同的页面。由于我必须做一些操作,我必须通过JavaScript函数来完成。JavaScript问题打开新页面

但它不工作,我得到这两个警报,但它不是导航到index.html(它与当前页面在同一级别),即使url不会改变。 你能帮我解决吗?

<html> 
    <head> 
     <title>Page Title</title> 
     <script type="text/javascript"> 
      function recordPickupDetails1(){ 
       alert("calling"); 
       location.href='index.html'; 
       alert("calling after"); 
     } 
    </script> 
</head> 
<body> 
    <form onsubmit="recordPickupDetails1()"> 
     <ul> 
      <li> 
       <h2>Pickup Details</h2> 
      </li> 
      <li> 
       <label for="name">Name:</label> 
       <input type="text" id="name" name="name" pattern="[A-Za-z ]*" required /> 
      </li> 
      <li>     
       <button class="submit" type="submit" > Next </button> 
      </li> 
     </ul> 
    </form> 
</body> 

+0

这种尝试:location.replace(“http://www.w3schools.com”); http://www.w3schools.com/jsref/met_loc_replace.asp –

回答

2

添加动作到表单中。你不需要JavaScript。

<form action="index.html"> 

http://jsfiddle.net/vm3m74q1/5/

小提琴被更新,所以你可以使用JavaScript做一些你之前提交。

+0

谢谢,但我需要做一些操作,以及内部的JS功能。 – Elike

0

如果你想要做一些操作,然后再决定是否提交应做或不尝试:

<script> 
function recordPickupDetails1(){ 

    alert("calling"); 
    // if you want stop function and submit use: 
    return false; 
    alert('call2'); 
} 
</script> 
<from action="index.htm" onsubmit="return recordPickupDetails1()">