我已经在Page1.html中使用jquery进行了一些计算,通过使用get函数从inputbox和inputbox1获取值,并使用Jquery的set函数在inputbox2中设置来存储变量Z及其值。现在我想在Page2.html中传递变量Z和它的值。请帮帮我。
Jquery Example
Page1.html
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get a time-based greeting:</p>
INCOME: <input type="text" id="myinput" value="">
DEDUCTION: <input type="text" id="myinput1" value="">
GROSS INCOME: <input type="text" id="myinput2" value="">
<button id="click1">CALCULATE NET INCOME</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#click1").click(function a(){
myfunction();
});
function myfunction() {
var x, y, z;
var x = $("#myinput").val();
var y = $("#myinput1").val();
z = x - y;
$("#myinput2").val(z);
}
</script>
</body>
</html>
Page2.html
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get a time-based greeting:</p>
INCOME: <input type="text" id="myinput3" value="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
</script>
</body>
</html>
发布于 2018-07-23 15:20:49
改变一些小东西就是你的myfunction函数:在最后添加更多的东西
function myfunction() { var x,y,z;var x= $("#myinput").val();var y= $("#myinput1").val();z=x- y;$("#myinput2").val(z);var form = document.createElement(" form ");// <--创建了一个表单元素form.setAttribute("yourData",z);// <--这是你的值form.setAttribute(“yourData”,"index2.php");// <--设置发送数据的路径form.submit();// <--发送数据}
下面是index2.php (发送数据的地方)中的一些代码:
<?php
$redeivedData = $_POST['yourData']; //<-- Your data received
?>
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get a time-based greeting:</p>
INCOME: <input type="text" id="myinput3" value=" <?php echo $redeivedData; ?>"> <!--I just put your data here .. -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
</html>发布于 2018-07-23 15:26:56
您可以使用cookies或localStorage来实现此目的。
使用Cookie的:
在两个页面中重复这些代码:
function updateCookie(name, value) {
document.cookie = name + "=" + value + "; expires=" + AddMin(5) + "; path=/";//path is neccessary 4 live this cookie in all similar address.
};
function deleteCoockie(name) {
document.cookie = name + "=; expires=" + (new Date(0)).toUTCString() + ";";
};
function readCookie(name) {
return ((document.cookie.match(new RegExp(name + " ?= ?[^=]*?([^;]*)", "g")) || [""])[0]).substr(name.length + 1);
};
function AddMin(m) {
var d = new Date(); d.setMinutes(d.getMinutes() + m);
return d.toUTCString();
};现在在您的myfunction中
updateCookie("VarName4Transfer", "DesiredValue4Transfer");在dest页面中:
var TransferedVariable2ThisPage = readCookie("VarName4Transfer");
deleteCoockie("VarName4Transfer");https://stackoverflow.com/questions/51473090
复制相似问题