1 回答

TA貢獻1827條經(jīng)驗 獲得超4個贊
有幾點需要注意:
您
selected
對所有選項都有支持你的 javascript 總是在 sessionStorage 中設(shè)置
currencyListFrom
andcurrencyListTo
元素的值,即使那里什么也沒有。這實際上將值設(shè)置為未定義。
解決這些問題似乎可以解決問題:
<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>
添加回答
舉報