2017-03-24 70 views
0

我想将文本框的值从一个html页面传递到另一个html页面并在第二页上打印。我有page1.html和page2.html。我不想使用ASP或PHP的任何服务器端脚本语言只是我想使用JavaScript或jQuery的任何人都很容易。如何将文本框值从一个html页面传递到另一个

<script> 
 
     function checkPassword() { 
 
      if (document.getElementById("name").value == "") { 
 
       document.getElementById("studname").innerHTML = "Enter your name. Field cannot be left blank."; 
 
       alert('Enter your name.'); 
 
       return false; 
 
      } 
 
      else if (document.getElementById("class").value == "select") { 
 
       document.getElementById("classname").innerHTML = "Select your class."; 
 
       alert('Select your class.'); 
 
       return false; 
 
      } 
 
      else if (document.getElementById("section").value == "select") { 
 
       document.getElementById("secname").innerHTML = "Select your section."; 
 
       alert('Select your section.'); 
 
       return false; 
 
      } 
 
      else if (document.getElementById("password").value == "") { 
 
       document.getElementById("passwordname").innerHTML = "Enter your password."; 
 
       alert('Enter your password.'); 
 
       return false; 
 
      } 
 
      else if (document.getElementById('password').value == '12345' && document.getElementById("class").value == 'V' && document.getElementById("section").value == 'a') { 
 
       location.href = 'Start.html?name=' + document.getElementById('name').value + '?class=' + document.getElementById('class').value; 
 
      } 
 
      else { 
 
       alert('Your Class, Section and Password doesn\'t match. Please re-enter correctly.'); 
 
       return false; 
 
      } 
 
     } 
 
     </script>

+0

查询字符串,localStorage的。 ? –

回答

0

运行这段代码,U会得到你所需要的东西输出。

html1.html

<html> 
<form type=get action="html2.html"> 
<table> 
<tr> 
<td>First Name:</td> 
<td><input type=text name=firstname size=10></td> 
</tr> 
<tr> 
<td>Last Name:</td> 
<td><input type=text name=lastname size=10></td> 
</tr> 
<tr> 
<td>Age:</td> 
<td><input type=text name=age size=10></td> 
</tr> 
<tr> 
<td colspan=2><input type=submit value="Submit"> 
</td> 
</tr> 
</table> 
</form> 
</html> 

html2.html

<html> 
<script LANGUAGE="JavaScript"> 
function getParams(){ 
var idx = document.URL.indexOf('?'); 
var params = new Array(); 
if (idx != -1) { 
var pairs = document.URL.substring(idx+1, document.URL.length).split('&'); 
for (var i=0; i<pairs.length; i++){ 
nameVal = pairs[i].split('='); 
params[nameVal[0]] = nameVal[1]; 
} 
} 
return params; 
} 
params = getParams(); 
firstname = unescape(params["firstname"]); 
lastname = unescape(params["lastname"]); 
age = unescape(params["age"]); 
document.write("firstname = " + firstname + "<br>"); 
document.write("lastname = " + lastname + "<br>"); 
document.write("age = " + age + "<br>"); 
</script> 
</html> 
相关问题