【发布时间】:2017-05-16 22:22:11
【问题描述】:
我正在开发一个报价页面和订单页面,它们使用会话存储将来自两个范围滑块的信息和报价带到下一页。
到目前为止,我的两个范围滑块都在工作,但发现报价输出元素有困难。
我不能使用 Id,因为我在订单页面上有两个具有相同值的输出元素,所以我需要使用一个类,但不确定这是否适用于 sessionStorage.getItem
我也不确定应该使用什么事件监听器来激活报价中的会话存储。
以下是两个 sn-ps,我在需要帮助的地方注释掉并使用了问号。
这是我报价页面的代码。
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>
这是我的订单页面上的代码。
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>
【问题讨论】:
-
getElementsByClassName返回的不是一个,而是所有具有此类的元素,所以你会得到几个。 -
getElementsByClassName不会为所有价格输出提供相同的值,这正是我所追求的,但即使尝试在订单页面上仅使用一个输出元素的getElementById也很困难 -
请在我的回答下留下一些 cmets,以便我修改。
标签: javascript html forms output session-storage