2014-10-04 91 views
0

我想成立一​​個JS當用戶按下按鈕,將其重定向到主頁,名爲index.html改變頁面

<form method="confirm" style="width: 500px; height: 300px; margin-left: auto; margin-right: auto; margin-top:100px; margin-bottom: 100px; font-size: 30px; color: black;"> 
<fieldset id="confirm"> 
    <legend>NOTIFICATION</legend> 
    <table> 
    <tr> 
     <td> 
     <h1>THANK YOU FOR SIGNING IN</h1> 
     </td> 
    </tr> 
     <td> 
    <input type="submit" value="Return to homepage" onclick="return return1();"> 
    <script> 
      function return1() 
      { 
       window.location.href = 'index.html'; 
      } 
    </script> 
     </td> 
    </tr> 


</table> 
</fieldset> 
    </form> 
+0

這裏有什麼問題? 。 。 。 。 – rorofromfrance 2014-10-04 08:09:01

+0

我認爲他的東西不起作用。 – 2014-10-04 08:09:38

+0

當我點擊按鈕時,它不會將我重定向到索引頁:) – 2014-10-04 08:09:53

回答

0

添加返回false的功能禁用默認事件在提交(頁面刷新)

function return1() 
     { 
      window.location.href = 'index.html'; 
      return false; 
     } 
+0

我確實添加了返回false但仍然不能正常工作:) – 2014-10-04 08:17:32

+0

您是否在控制檯中遇到錯誤? – 2014-10-04 08:19:14

+0

到目前爲止沒有錯誤 – 2014-10-04 08:19:54

0

更換

window.location.href = 'index.html'; 

有:

window.open('INDEX.HTML'); 
0

下面的代碼我重定向到index.php這在同級別的index.html坐着index.js

<!DOCTYPE html> 
    <html lang="en"> 
     <head> 
      <meta charset="UTF-8"> 
      <title>Document</title> 
     </head> 
     <body> 

      <form action="check.php" method="post" id="re"> 
       <fieldset id="confirm"> 
       <legend>NOTIFICATION</legend> 
       <table> 
       <tr> 
       <td> 
       <h1>THANK YOU FOR SIGNING IN</h1> 
       </td> 
       </tr> 
       <td> 
       <input type="submit" value="Return to homepage"> 
       </td> 
       </tr> 


       </table> 
       </fieldset> 
      </form> 

       <script src="index.js"></script> 
     </body> 
     </html> 

JS文件

function _x(elem){ 
    return document.getElementById(elem); 
} 

var x = _x("re"); 
    x.onsubmit = function(e){ 
     e.preventDefault(); 
     window.location = "index.php"; 
} 
1

有一些失誤這裏。

你得到的錯誤是因爲你用一個表單內提交輸入按鈕,該按鈕將試圖評估您的表單提交。爲了防止你必須在你的函數上返回false。

<script> 
     function return1() 
     { 
      window.location.href = 'index.html'; 
      return false; 
     } 
</script> 

但是,如果你想要做一個按鈕,簡單地重定向到另一個頁面,你不應該使用形式和一個提交按鈕,但只是一個普通的輸入按鈕

<input type="button" value="Return to homepage" onclick="return1();"> 

如果你想使用的形式,因爲要計算一些數據,你需要把你的頁面上的action領域<form>,而無需使用腳本

也證實是不能接受的,你應該使用GETPOST

<form method="POST" action="index.html"> 
0

簡單地改變你的輸入型submitbutton

<input type="button" value="Return to homepage" onclick="return return1();"> 
0

您更改代碼

<input type="submit" value="Return to homepage" onclick="return return1();"> 

=><a href="index.html" class="btn">Return to homepage</a>

以鍵入CSS 「.btn」 看到像按鈕

注意:在表單中,如果不提交表單,則不應放置或重定向。

0

這可能會幫助你

<script language="javascript"> 

function return1() 
{ 
    window.location="yourpage.html"; 
} 
</script>