2016-08-13 59 views
0

因此,我做了一個簡單的頁面,其中提交按鈕應該重定向到另一個URL。這是我的代碼。無法通過JS重定向到另一個URL

HTML:

<form onsubmit="javascript:redirectRoll();"> 
      <input type="text" id="key" name="Roll" placeholder="Enter the full Roll No" size="60" pattern=".{10,}" required title="Every Roll No is exactly of 10 characters" maxlength="10" autofocus required><br></span></input><br> 
      <input type="submit" name="submit"></input> 
      </form> 

JS

function redirectRoll(){ 
window.location.href="www.example.com"; 
} 

每當我點擊提交,它只是把我帶回到同一頁面。請幫助!

好吧,我張貼我剛剛主持了網站鏈接: jbresults.site88.net/results.html

這應該很容易讓你們發現問題:)

+1

'location.href'設置將被忽略。使用輸入元素類型的按鈕和點擊處理程序。 – Teemu

回答

0

這項工作對我來說:

function redirectRoll(){ 
    window.location ="www.example.com"; 
} 

你可能會改變你調用的函數 在HTML的方式

<form> 
     <input type="text" id="key" name="Roll" placeholder="Enter the full Roll No" size="60" pattern=".{10,}" required title="Every Roll No is exactly of 10 characters" maxlength="10" autofocus required><br></span></input><br> 
     <input type="submit" name="submit" onclick="redirectRoll();"></input> 
</form> 
+0

已經嘗試過。沒有幫助。 – Varun

1

正如@teemu所評論的,表單提交覆蓋了重定向。您需要爲您的redirectRoll函數使用以下代碼。請務必將onsubmit值更改爲"redirectRoll(event)"

function redirectRoll(e) { 
    e.preventDefault(); 
    location.href = "example.com"; 
} 
0

剛剛嘗試,而到服務器的呼叫待該

function redirectRoll() 
{ 
    $(location).attr('href','www.example.com'); 
} 
相關問題