2017-10-10 45 views
0

如何將文件鏈接在一起?我的意思是,我該如何創建一個按鈕,點擊後將您帶到另一個網站? (或在我的情況下,閱讀的下一頁)對不起,愚蠢的問題,我是新來的編碼,我只知道基於密碼的按鈕。 :(將文件鏈接在一起

password: <input type=password ID="Next"> <button onclick="correctpassword();">submit</button> 
    </div> 
    <script type="text/javascript"> 
     function correctpassword() { 
      var code = document.getElementById ("Next").value; 
      // alert ("Haha! I know your password! It's \"" + code + "\""); 
      if (code == "Next") { 
       location = "NHD2.html"; 
      } else if (code == "next") 
       alert ("So Close!!"); 
      else location = "LoginWrongSite2.html"; 
     } 

是我。

+2

當你說你想創建一個鏈接按鈕,爲什麼不創建一個純HTML鏈接而不使用JavaScript? '

button

' – Vincent1989

+0

DiegoTArgs的回答很好,同樣參見https://stackoverflow.com/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link更多選項 – visibleman

回答

0

我想你想模擬一個小表格。

如果這是你試圖做什麼,然後試試這個(一定要有兩個頁面在相同的目錄):

Page1.html

<!DOCTYPE html> 
<html> 
<head> 
    <title>Page1</title> 
</head> 
<body> 

    <form onsubmit="submitform(event)" action="Page2.html"> 
    Name: <input type="text" placeholder="Your name here" id="username"/> 
    Password <input type="password" id="userpassword"/> 
    <button type="submit">Submit</button> 
    </form> 

    <script type="text/javascript"> 
    function submitform(event) { 
     var username = document.getElementById('username'); 
     var userpassword = document.getElementById('userpassword'); 
     if(username.value !== 'test' || userpassword.value !== '1234') { 
     event.preventDefault(); 
     } 
    } 
    </script> 

</body> 
</html> 

Page2.html

<!DOCTYPE html> 
<html> 
<head> 
    <title>Page1</title> 
</head> 
<body> 

    We are good ! 

</body> 
</html> 

這將基本上在page1中定義一個表單。 當您在該頁面中輸入test作爲用戶名並輸入1234作爲密碼時,它將被提交,您將導航到哪個操作屬性所說的內容。 如果輸入的值不同於ussername的測試值,或者密碼導航1234的值將被取消。

當然這只是一個簡單的例子,在一個真實的應用程序中,你不會做這樣的事情,它只是讓你知道如何從page1導航到page2(還有其他方式,例如鏈接)。

希望這會有所幫助!

+0

是的,它確實,它甚至詢問我是否想爲網站保存密碼,但事實是,我沒有看到代碼中的位置,它會將您轉到下一頁,如果它已經存在,請告訴哪一部分。 – MAXPower

+0

當你點擊提交按鈕時,submitform函數將會執行。如果輸入正確的數據(測試和1234),表單的正常行爲將不會被取消,您將導航。通過正常的行爲,我的意思是,當通過點擊像我聲明的提交按鈕來提交表單時,表單數據將被提交給動作屬性中指明的url。所以這就是你導航到下一頁的原因。我希望這可以幫助你。 – DiegoTArg

+0

是的,非常。感謝您的幫助。 – MAXPower