将本地存储的定价添加到其他字段时遇到问题。如果我将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');
答案 0 :(得分:0)
您正在尝试解析一个空字符串,因此您正在尝试解析空字符串,因此要么在代码中添加一个特殊情况,在格式化之前检查字符串是否为空(默认为[not]设置]或者其他东西),或者你将0设置为默认值,这样可以将其解析回来,你得到0美元,有意义吗?