gpt4 book ai didi

javascript - localStorage 项与其他 localStorage 项冲突

转载 作者:行者123 更新时间:2023-11-28 08:04:17 26 4
gpt4 key购买 nike

我在将本地存储的定价添加到其他字段时遇到问题。如果我将 beforeNoonNPSlot 的价格设置为 2,但我将 matineeNPSlot 的字段留空(它应该保留分配给的任何价格)最后)。然后在我更新更改后。 beforeNoonPrice 应该是 $2.00,但 matineeNPSlot$NaN。另外,如果我为 beforeNoonNPSlot 设置价格并为 matineeNPSlot 设置价格,则不存在 NaN 问题。

我希望可以选择为任何字段(不一定是所有字段)设置价格,而不会出现此问题。如何防止这种情况发生?

function updatePricingFunction() {
var beforeNoonFieldChange = document.getElementById('beforeNoonNPSlot').value;
var matineeFieldChange = document.getElementById('matineeNPSlot').value;

localStorage.setItem('text', beforeNoonFieldChange);
localStorage.setItem('text1', matineeFieldChange);
}

function load() {
var storedValue = localStorage.getItem('text');
var storedValue1 = localStorage.getItem('text1');

beforenoonprice = storedValue;
matineeprice = storedValue1;
beforeNoonCPSlot.innerHTML = "$" + parseFloat(storedValue).toFixed(2);
$("#beforeNoonPrice").attr = parseFloat(storedValue).toFixed(2);

$('#beforeNoonPrice').append("$" + (storedValue * 1).toFixed(2));

matineeCPSlot.innerHTML = "$" + parseFloat(storedValue1).toFixed(2);
$("#matineePrice").attr = parseFloat(storedValue1).toFixed(2);

$('#matineePrice').append("$" + (storedValue1 * 1).toFixed(2));
}



// JavaScript Document
var beforenoonprice = 6.75; // CHANGE THE PRICE OF THE BEFORE NOON TICKET
var matineeprice = 9.00; // CHANGE THE PRICE OF THE MATINEE TICKET
var seniorprice = 9.25; // CHANGE THE PRICE OF THE SENIOR TICKET
var militaryprice = 9.25; // CHANGE THE PRICE OF THE MILITARY TICKET
var studentdayprice = 8.00; // CHANGE THE PRICE OF THE STUDENT DAY TICKET
var seniordayprice = 6.75; // CHANGE THE PRICE OF THE SENIOR DAY TICKET
var adultprice = 10.75; // CHANGE THE PRICE OF THE ADULT TICKET
var childprice = 8.00; // CHANGE THE PRICE OF THE CHILD TICKET
var threeDprice = 3.50; // CHANGE THE PRICE OF THE REGULAR 3D PRICE
var imaxPrice = 4.50; // CHANGE THE PRICE OF THE IMAX TICKET
var imax3dPrice = 5.50; // CHANGE THE PRICE OF THE IMAX 3D TICKET
var output = document.getElementById('output');

最佳答案

你得到NaN是因为你试图解析一个空字符串,所以要么你在代码中添加一个特殊情况,在格式化之前检查字符串是否为空(默认为[未设置]或其他东西) ),或者你设置 0 作为默认值,这样它就可以被解析回来,你得到 $0,有意义吗?

关于javascript - localStorage 项与其他 localStorage 项冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24977068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com