2015-12-30 41 views
-2

我是HTML新手。我已經寫了下面的代碼來從其中一個選項中進行選擇。選擇其中一個單選按鈕並單擊提交按鈕後,用戶應該被重定向到考試頁面,其中的單選按鈕類型的候選類型將在URL中傳遞,該頁面必須在下一頁提取。將單選按鈕的值從一個HTML頁面傳遞到另一個作爲參數,並在下一個HTML頁面上提取它

有人可以幫助我通過這個要求來傳遞價值和提取它在下一頁。

<html> 
    <head> 
     <title>Online Examination Portal</title> 
     <h1>Online Examination</h1> 
     <script type="text/javascript"> 
      function get_action(form) { 
      form.action = document.querySelector('input[name = "candidateType"]:checked').value; 
     } 
     </script> 
    </head> 
    <body> 
     <div>Select candidate type from below option:<br><br> 
      <div> 
       <input type="radio" name="candidateType" value="student">Student 
       <br> 
       <input type="radio" name="candidateType" value="professional">Professional 
       <br><br> 
       <form action="ExamPage.html" method="get"><input type="submit" value="Submit" onclick="get_action(this);"></form>   
       <br><br> 
       <form action="RegistrationPage.html" method=post name="form2"><input type="submit" value="Register"></form> 
      </div> 
     </div> 
    </body> 
</html> 
+0

人,你在嗎? –

+0

[將表單數據傳遞給另一個HTML頁面]的可能重複(http://stackoverflow.com/questions/14693758/passing-form-data-to-another-html-page) – Trevor

回答

2

您可以使用餅乾或LocalStorage,其中LocalStorage更容易實現,但需要最新的瀏覽器,用戶可以禁用cookie隱私的原因。

餅乾

function setCookie(name, value, days) { 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime()+(days*24*60*60*1000)); 
     var expires = "; expires="+date.toGMTString(); 
    } 
    else var expires = ""; 
    document.cookie = name+"="+value+expires+"; path=/"; 
} 

function getCookie(name) { 
    var nameEQ = name + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0;i < ca.length;i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1,c.length); 
     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
    } 
    return null; 
} 

// First Page 
setCookie("myinputvalue", document.getElementsByName("candidateType")[0].value, 10); 

// Second Page 
getCookie("myinputvalue"); 

localStorage的

if (typeof(Storage) !== "undefined") { 
    // First Page 
    localStorage.setItem("myinputvalue", document.getElementsByName("candidateType")[0].value); 
    // Second Page 
    localStorage.getItem("myinputvalue"); 
} else { 
    // Sorry! No Web Storage support.. 
    // Use the above cookie method. 
} 
相關問題