我不确定错误是什么(Uncaught TypeError:无法读取未定义的属性'basePrice')意味着因为该值在第一部分中已明确定义。
当我展开错误消息时,它标记更新的价格,然后标记两个匿名函数..我不确定我是否应该查找拼写错误或者我是否使用了错误的逻辑。
<script>
(function() {
var plans = {
basic: {
pricingChart: false,
pricingChart2: false,
basePrice: 0
},
standard: {
pricingChart: false,
pricingChart2: false,
basePrice: 2.99
},
premium: {
pricingChart: true,
pricingChart2: false,
basePrice: 49.99
},
enterprise: {
pricingChart: true,
pricingChart2: true,
basePrice: 500
}
};
var viewerLevels = {
500: 10.99,
2500: 10.99,
5000: 29.99,
10000: 39.99
};
var userLevels = {
1: 10.99,
2: 20.99,
3: 30.99,
4: 40.99
};
var selectedPlan = "free";
var viewerLevel = null;
var userLevel = null;
$( ".plan .select-button" ).on( "click", function( event ) {
selectedPlan = $( this ).closest( ".plan" ).attr( "data-plan" );
var planInfo = plans[ selectedPlan ];
if ( planInfo.pricingChart ) {
$( "#pricingChart" ).removeClass( "not" );
} else {
$( "#pricingChart" ).addClass( "not" );
viewerLevel = null;
}
if ( planInfo.pricingChart2 ) {
$( "#pricingChart2" ).removeClass( "not" );
} else {
$( "#pricingChart2" ).addClass( "not" );
userLevel = null;
}
var showPricingChart = "#topPricingChart";
if ( planInfo.pricingChart ) {
showPricingChart = "#pricingChart";
}
$( "html,body" ).animate({
scrollTop: $( showPricingChart ).offset().top
}, "slow" );
updatePrice();
});
$( ".pricingLevel" ).on( "click", function() {
viewerLevel = $( this ).attr( "data-viewer" );
updatePrice();
});
$( ".pricingLevel2" ).on( "click", function() {
userLevel = $( this ).attr( "data-user" );
updatePrice();
});
function updatePrice() {
var basePrice = plans[ selectedPlan ].basePrice;
var viewerPrice = 0;
if ( viewerLevel ) {
viewerPrice = viewerLevels[ viewerLevel ];
}
var userPrice = 0;
if ( userLevel ) {
userPrice = userLevels[ userLevel ];
}
var totalPrice = basePrice + viewerPrice + userPrice;
$( ".pricingLargeAmount" ).text( totalPrice );
}
updatePrice();
})();
</script>
答案 0 :(得分:2)
错误表示未定义plans[ selectedPlan ]
。
selectedPlan == "free"
和plans.free
不存在。
答案 1 :(得分:0)
该错误表示selectedPlan
的值不是plans
的属性名称。例如,您要将其初始化为"free"
,但plans
没有此类属性。 plans["free"]
为undefined
,因此您“无法读取未定义的属性'basePrice'”