简体   繁体   English

会话存储多个输出字段

[英]Session Storage multiple output fields

I'm working on a quote page and order page that use session storage to bring the information from two range sliders and the quoted price into the next page. 我正在使用会话存储的报价页面和订单页面上,将来自两个范围滑块和报价的信息带到下一页。

So far I have both range sliders working but am finding difficulty with the quoted price output element. 到目前为止,我都可以使用两个范围滑块,但是在报价输出元素方面遇到了困难。

I cant use an Id as I have two output elements with the same value on the order page so I would need to use a class but am not sure if this would work with sessionStorage.getItem 我不能使用Id,因为我在订单页面上有两个具有相同值的输出元素,因此我需要使用一个类,但不确定是否可以与sessionStorage.getItem

I am also not sure what event listener I should use to activate the session storage on the quoted price. 我也不确定应该使用哪个事件侦听器来激活报价的会话存储。

below are two snippets and I have commented out and used question marks where I need help. 以下是两个摘要,我已注释掉并在需要帮助的地方使用问号。

This is the code from my quote page. 这是我报价页面中的代码。

 if (true) { var weightOut = document.getElementById('weightOutputId'), weightIn = document.getElementById('weightInputId'), distanceOut = document.getElementById('distanceOutputId'), distanceIn = document.getElementById('distanceInputId'); //calPrice = document.getElementsByClassName('calP'); ??? weightOut.value = sessionStorage.getItem('weightOutputId'); weightIn.value = sessionStorage.getItem('weightInputId'); distanceOut.value = sessionStorage.getItem('distanceOutputId'); distanceIn.value = sessionStorage.getItem('distanceInputId'); //calPrice.value = sessionStorage.getItem('calP'); ??? // window.onload = function (){ ?? // sessionStorage.setItem('calP', calPrice.value); ??? //} ?? weightIn.addEventListener('input', function () { sessionStorage.setItem('weightOutputId', weightOut.value); sessionStorage.setItem('weightInputId', weightIn.value); }, false); distanceIn.addEventListener('input', function () { sessionStorage.setItem('distanceOutputId', distanceOut.value); sessionStorage.setItem('distanceInputId', distanceIn.value); }, false); } 
 <form action="#" method="post" oninput="quote.value = distanceInputId.value * weightInputId.value +4;"> <label for="weightOutputId" class="formQ"> Weight (kg): </label> <output name="weightOutputName" id="weightOutputId"> 1 </output> <input type="range" name="weightInputName" id="weightInputId" value="1" min="1" max="10" oninput="weightOutputId.value = weightInputId.value" class="formR"> <label for="distanceInputId" class="formQ"> Distance (km): </label> <output name="distanceOutputName" id="distanceOutputId"> 1 </output> <input type="range" name="distanceInputName" id="distanceInputId" value="1" min="1" max="20" oninput="distanceOutputId.value = distanceInputId.value" class="formR"> <span class="calP"> Calculated Price: € </span> <output name="quote" class="calP"> 5 </output> </form> 

This is the code from my order page. 这是我的订单页面中的代码。

 if (true) { var weightOut = document.getElementById('weightOutputId'), weightIn = document.getElementById('weightInputId'), distanceOut = document.getElementById('distanceOutputId'), distanceIn = document.getElementById('distanceInputId'); //calPrice = document.getElementsByClassName('calP'); ??? weightOut.value = sessionStorage.getItem('weightOutputId'); weightIn.value = sessionStorage.getItem('weightInputId'); distanceOut.value = sessionStorage.getItem('distanceOutputId'); distanceIn.value = sessionStorage.getItem('distanceInputId'); //calPrice.value = sessionStorage.getItem('calP'); ??? // window.onload = function (){ ?? // sessionStorage.setItem('calP', calPrice.value); ??? //} ?? weightIn.addEventListener('input', function () { sessionStorage.setItem('weightOutputId', weightOut.value); sessionStorage.setItem('weightInputId', weightIn.value); }, false); distanceIn.addEventListener('input', function () { sessionStorage.setItem('distanceOutputId', distanceOut.value); sessionStorage.setItem('distanceInputId', distanceIn.value); }, false); } 
 <form id="contact_form" action="#" method="post" oninput="quote.value = distanceInputId.value * weightInputId.value +4, quoteT.value = distanceInputId.value * weightInputId.value +4;"> <label for="weightInputId" class="formQ"> Weight (kg): </label> <output name="weightOutputName" id="weightOutputId"> 1 </output> <input type="range" name="weightInputName" id="weightInputId" value="1" min="1" max="10" oninput="weightOutputId.value = weightInputId.value" class="formR"> <label for="distanceInputId" class="formQ"> Distance (km): </label> <output name="distanceOutputName" id="distanceOutputId"> 1 </output> <input type="range" name="distanceInputName" id="distanceInputId" value="1" min="1" max="20" oninput="distanceOutputId.value = distanceInputId.value" class="formR"> <span class="calP"> Calculated Price: € </span> <output name="quote" class="calP" id="keepNum"> 5 </output> <!-- the rest of my form goes here--> <span class="calP"> Calculated Price: € </span> <output name="quoteT" class="calP"> 5 </output> </form> 

Here is my first try, let's edit it together. 这是我的第一次尝试,让我们一起编辑它。

'use strict'; // Strict mode is a good thing.

// ... Somehow you get a value for your price and store it:

const calculatedPrice = ...;
sessionStorage.setItem('calP', calculatedPrice)

// ... Then you read it and fill multiple fields with it.

let calculatedPrice = parseFloat(sessionStorage.getItem('calP'));
if (!calculatedPrice && calculatedPrice !== 0) {
  calculatedPrice = ...; // Get it somehow.
  sessionStorage.setItem('calP', calculatedPrice); // Update it.
}

// Now render all the fields!
const elements = Array.from(document.getElementsByClassName('calP'));
elements.forEach(function(el) { el.innerText = calculatedPrice; })

el.innerText fills the whole content of an element. el.innerText填充元素的全部内容。 If you need to fill it like a template with "holes" then it will require more code — ask me for it). 如果您需要像模板一样用“洞”填充它,那么它将需要更多代码-询问我)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM