我目前在使用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/>
感谢您的帮助!
使用网络存储。
sessionStorage-存储一个会话的数据
sessionStorage.getItem('label')
sessionStorage.setItem('label', 'value')
localStorage-存储没有到期日期的数据
localStorage.getItem('label')
localStorage.setItem('label', 'value')
参考
window.location = http://mysite/page2.html?amount=12
searchParams
属性从查询字符串中检索值(请参见URL.searchParams)。可能看起来像:let params = (new URL(document.location)).searchParams;
document.getByElementId('text1').value = params.get('amount');
这里是一个使用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">
希望有帮助