将多个值从一个html页面传递到另一html页面

时间:2019-01-14 10:12:09

标签: jquery

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

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()+$('#myinput4').val();
	});

	}
 

});
</script>

</body>
</html>


Page2.html

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

</style>
</head>


<body>
TOTAL INCOME: <input type="text" id="myinput9" value="" readonly>
NET INCOME: <input type="text" id="myinput10" value="">
EMI CAR: <input type="text" id="myinput11" value="">
TOTAL 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, tax8, tax9, tax10;

var x = document.URL.indexOf('?tax7=');
	
	$("#myinput9").val(x);	

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

2 个答案:

答案 0 :(得分:0)

这里您使用的是document.URL.indexOf('?tax7='),它会给您在url中的字符串位置。

您可以检查以下步骤以找到一个值。

var url_string = window.location.href; // give u current url
var url = new URL(url_string); // create url string
var c = url.searchParams.get("tax7"); // search your parameter

答案 1 :(得分:0)

我如下更改了您的代码。 如果要输入inputbox9和inputbox10的值,则要分别发送。

window.location.href="2.html?tax7="+$('#myinput8').val()+"&tax10="+$('#myinput4').val();

如果要获取url参数值,请使用上述修改

var urlParams = new URLSearchParams(window.location.search);
        tax9 = urlParams.get('tax7');
    tax10 = urlParams.get('tax10');
    $("#myinput9").val(tax9);   
    $("#myinput10").val(tax10); 

您可以在下面找到完整的代码。

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="2.html?tax7="+$('#myinput8').val()+"&tax10="+$('#myinput4').val();
    });

    }


});
</script>

</body>
</html>

Page2.html

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

</style>
</head>


<body>
TOTAL INCOME: <input type="text" id="myinput9" value="" readonly>
NET INCOME: <input type="text" id="myinput10" value="">
EMI CAR: <input type="text" id="myinput11" value="">
TOTAL 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, tax8, tax9, tax10;


var urlParams = new URLSearchParams(window.location.search);
    tax9 = urlParams.get('tax7');
    tax10 = urlParams.get('tax10');
    $("#myinput9").val(tax9);   
    $("#myinput10").val(tax10); 

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