1 回答
TA贡献1797条经验 获得超6个赞
有几点需要注意:
你
selected
对所有的选择都有支持currencyListFrom
你的 javascript 总是设置 sessionStorage 中和元素的值currencyListTo
,即使那里什么也没有。这实际上是将值设置为未定义。
解决这些问题似乎可以解决问题:
<label for="currencyListFrom">From:</label>
<select
id="currencyListFrom"
onchange="callCurrFrom(this)"
name="cf"
form="currencyform"
>
<option value="none" disabled hidden>Select Currency From</option>
<option value="EUR" label="EURO">EUR</option></select
>
<label for="currencyListTo">To:</label>
<select
id="currencyListTo"
onchange="callCurrTo(this)"
name="ct"
form="currencyform"
>
<option value="none" disabled hidden>Select Currency To </option>
<option value="USD" label="US dollar">USD</option>
</select>
<script>
// wrap in IIFE in order to not pollute global scope
(function() {
//sessionStorage stores the data for one session until web browser is open
function callCurrFrom(obj) {
//setItem() accept key, keyvalue pair
sessionStorage.setItem(
"selectedCur",
obj.options[obj.selectedIndex].value
);
// id value stored
document.getElementById("currencyListFrom").value =
obj.options[obj.selectedIndex].value;
}
var defaultFrom = sessionStorage.getItem(
"selectedCur"
);
if (defaultFrom) { // ensure it's not falsey
document.getElementById("currencyListFrom").value = defaultFrom
}
function callCurrTo(obj) {
sessionStorage.setItem(
"selectedCurr",
obj.options[obj.selectedIndex].value
);
document.getElementById("currencyListTo").value =
obj.options[obj.selectedIndex].value;
}
var defaultTo =sessionStorage.getItem(
"selectedCurr"
);
if (defaultTo) { // ensure it's not falsey
document.getElementById("currencyListTo").value = defaultTo
}
})();
</script>
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报