将变量的值从一个html页面传递到另一html页面

时间:2019-01-07 10:15:25

标签: jquery

我已经在Page1.html中使用jquery进行了一些计算,方法是使用get函数从输入框中获取值,并使用jquery的set函数在inputbox8中进行设置来存储变量tax6及其值。我已经使用window.location.href在Page2.html中的变量tax7中传递inputbox8的值。在Page2.html中,我使用了document.url.indexof,但是在inputbox9中没有得到该值。

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="" readonly>
GROSS INCOME: <input type="text" id="myinput2" value="" readonly>
TAX: <input type="text" id="myinput3" value=""readonly>
NET INCOME: <input type="text" id="myinput4" value="">

<button id="click1">CALCULATE NET INCOME</button><br><br>

NET INCOME: <input type="text" id="myinput5" value="">
AGRI INCOME: <input type="text" id="myinput6" value="">
OTHER SOURCE: <input type="text" id="myinput7" value="">
TOTAL INCOME: <input type="text" id="myinput8" value="">

<button id="click2">CALCULATE TOTAL INCOME</button>

<button id="click3">next</button>


<p id="demo"></p>

<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 tax, tax1, tax2;
var inc = $("#myinput").val();
    alert(inc);
     $("#myinput1").val(10000);

     tax1 = inc - 10000;

     $("#myinput2").val(tax1);

        if (tax1 <=250000) {
        tax = "0";
     } 

    else if (tax1 >250000 && tax1<=500000) {
    tax = (tax1 - 250000)*5/100;
     }

    else if (tax1 >500000 && tax1<=1000000) {
    tax = 12000 + ((tax1 - 500000)*20/100);
     }

    else { tax = 112000 + ((tax1 - 1000000)*30/100);
     }

    $("#myinput3").val(tax);


    tax2 =  tax1 - tax;


    $("#myinput4").val(tax2);
    $("#myinput5").val(tax2);


}

 $("#click2").click(function b(){


        myfunction1();

    });
    function myfunction1() {
var tax4, tax5, tax6, tax7;
     var ni=$("#myinput4").val();


    var tax4 = $("#myinput6").val();
    var tax5 = $("#myinput7").val();


m=parseInt(ni); 
k=parseInt(tax4);
l=parseInt(tax5);
    tax6 = k + l+m;

    $("#myinput8").val(tax6);       

    $("#click3").click(function c(){
        window.location.href="getset-index2.html?tax7="+$('#myinput8').val();
    });

    }


});
</script>

</body>
</html>

Page2.html

<!DOCTYPE html>
<html>
<head>
<style>

</style>
</head>


<body>
TOTAL INCOME: <input type="text" id="myinput9" value="">
EMI HOUSE: <input type="text" id="myinput10" value="">
EMI CAR: <input type="text" id="myinput11" value="">
NET INCOME: <input type="text" id="myinput12" value="">

<button id="click4">CALCULATE NET INCOME</button><br><br>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

    $("#click4").click(function d(){


            myfunction2();
    });
        function myfunction2() {

var x, tax7, tax8, tax9;
var x = document.URL.indexof('?tax7=');
alert(x);
        $("#myinput9").val(x);

}
});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

使用以下功能:

var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = window.location.search.substring(1),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
            }
        }
    };

并命名为:

var x = getUrlParameter('tax7');