一个页面通过URL向另一个页面传递简单数据2019-01-21 来源:蓝典网络
内容摘要:test1.html !DOCTYPEhtml html head meta charset = utf-8 title test1.html页面 / title script type = text/javascript src = ../js/jquery-1.11.1.js / script / head body 请输入: input type = text id = txta br / 第一种方式,传输入的...
test1.html
-
<!DOCTYPE html>
-
<html>
-
<head>
-
<meta charset="utf-8">
-
<title>test1.html页面</title>
-
<script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
-
</head>
-
<body>
-
请输入:
-
<input type="text" id="txta" >
-
<br />
-
第一种方式,传输入的值
-
<input type=button value="去往test2.html页面" onclick="tiaozhuan()" />
-
<script type="text/javascript">
-
var tiaozhuan = function(){
-
document.location.href ='test2.html?mydata1=' + $("#txta").val();
-
}
-
</script>
-
<br />
-
第二种方式,传固定的值
-
<input type=button value="去往test2.html页面" onclick="document.location.href ='test2.html?mydata1=ciweigudingzhi';" />
-
</body>
-
</html>
test2.html
-
<!DOCTYPE html>
-
<html>
-
<head>
-
<meta charset="utf-8">
-
<title>test2.html页面</title>
-
</head>
-
<body>
-
test1.html页面传来的值为:<input type=text id="txt1" />
-
<script type="text/javascript">
-
var txt1 = document.getElementById("txt1");
-
txt1.value = location.search.match(new RegExp("[\?\&]mydata1=([^\&]+)", "i"))[1];
-
</script>
-
</body>
-
</html>