在另一页上显示'sum'

问题描述 投票:0回答:3

我目前在使用javascript / html的下一页上显示计算出的总和时遇到问题。

我定义总和的计算看起来像这样,最后显示'sum +€'。

function udregnPant() {


    let sum = 0;
    for (i = 0; i <= pantListParsed.length; i++) {


        let totalPantKr = pantListParsed[i].aPantMoney + pantListParsed[i].bPantMoney + pantListParsed[i].cPantMoney;
        sum += totalPantKr;

        console.log(sum);
        document.getElementById('sumAfPantB').innerHTML = sum + " €.";
    }


}

在下面的HTML输入中,我想将总和显示为值,而不是'10€'

 <input type="text" name="amount" id="text1" value="10 €." readonly/>

感谢您的帮助!

javascript html loops
3个回答
1
投票

使用网络存储。

sessionStorage-存储一个会话的数据

sessionStorage.getItem('label')
sessionStorage.setItem('label', 'value')

localStorage-存储没有到期日期的数据

localStorage.getItem('label')
localStorage.setItem('label', 'value')

Example at JS Bin

参考

Share data between html pages

HTML5 Web Storage


0
投票
  1. 计算完您的值后,将用户重定向到查询字符串中带有值的URL(请参阅Redirections in HTTP)-这可能看起来像]
window.location = http://mysite/page2.html?amount=12
  1. 在新页面上,使用searchParams属性从查询字符串中检索值(请参见URL.searchParams)。可能看起来像:
let params = (new URL(document.location)).searchParams;
document.getByElementId('text1').value = params.get('amount');

0
投票

这里是一个使用local Storage的小例子,因为这样不允许本地存储在jsfiddle中尝试它,并且代码示例:

document.getElementById('btnSend').onclick = ()=>{
let total = document.getElementById('txtTotal').value;
if(!isNaN(total) && total > 0){
localStorage.setItem('total', total);
document.getElementById('txtTotal').value = '';
}
}
document.getElementById('btnLastTotal').onclick = ()=>{
var lastTotal = localStorage.getItem('total');
if(lastTotal !=undefined){
alert('last total is:'+lastTotal);
}else{
alert('No records found');
}
}
<input type="text" id="txtTotal">
<br>
<input type="button" id="btnSend" value="Save the total"> <input type="button" id="btnLastTotal" value="get Last Total">

希望有帮助

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.