所以基本上,请检查该代码 -为什么计算代码不起作用?
全码 -
<script type="text/javascript">
$(document).ready(function() {
var seasonLookup = [
{startDay: 1, startMonth:1, endDay: 10, endMonth: 6, season:1},
{startDay: 21, startMonth:9, endDay: 31, endMonth: 12, season:1},
{startDay: 11, startMonth:6, endDay: 30, endMonth: 6, season:2},
{startDay: 1, startMonth:9, endDay: 20, endMonth: 9, season:2},
{startDay: 1, startMonth:7, endDay: 31, endMonth: 8, season:3},
];
var cars = $('#cars_input').val();
var priceone = ($('#cars_input option[value="'+cars+'"]').attr('priceone'));
var pricetwo = ($('#cars_input option[value="'+cars+'"]').attr('pricetwo'));
var pricethree = ($('#cars_input option[value="'+cars+'"]').attr('pricethree'));
var pricefour = ($('#cars_input option[value="'+cars+'"]').attr('pricefour'));
var pricefive = ($('#cars_input option[value="'+cars+'"]').attr('pricefive'));
var pricesix = ($('#cars_input option[value="'+cars+'"]').attr('pricesix'));
var priceseven = ($('#cars_input option[value="'+cars+'"]').attr('priceseven'));
var priceeight = ($('#cars_input option[value="'+cars+'"]').attr('priceeight'));
var pricenine = ($('#cars_input option[value="'+cars+'"]').attr('pricenine'));
var priceMatrix = {
cars: {
1: { t1: priceone, t2: pricetwo, t3: pricethree},
2: { t1: pricefour, t2: pricefive, t3: pricesix},
3: { t1: priceseven, t2: priceeight, t3: pricenine}
}
};
function getSeason(date){
var day = date.getDate();
var month = date.getMonth()+1;
var year = date.getFullYear();
for(var i=0;i<seasonLookup.length;i++){
var s = seasonLookup[i];
var startDate = new Date(year, s.startMonth-1,s.startDay);
var endDate = new Date(year, s.endMonth-1,s.endDay);
if(date >= startDate && date <= endDate)
return s.season;
}
return null;
}
function getPrice(bike, season, days){
var tier = "";
if(days <=2)
tier = "t1";
else if(days <=7)
tier = "t2";
else
tier = "t3"
console.log(days + ' days in season ' + season + ' at ' + priceMatrix[bike][season][tier] + '/day (' + tier + ')')
return priceMatrix[bike][season][tier] * days;
}
function calculatePrice(startDate, endDate, bike)
{
console.log(startDate);
console.log(endDate);
var currentSeason = getSeason(startDate);
var totalPrice = 0;
var daysInSeason = 0;
var currentDate = startDate;
while(currentDate<=endDate){
var season = getSeason(currentDate);
if(season != currentSeason){
totalPrice += getPrice(bike,currentSeason,daysInSeason);
currentSeason = season;
daysInSeason = 0;
}
daysInSeason++;
currentDate.setDate(currentDate.getDate()+1);
}
totalPrice += getPrice(bike,currentSeason,daysInSeason);
return totalPrice;
}
$('.recalc').change(function(){
var startDate = new Date(parseInt($('#year_input').val(),10),parseInt($('#month_input').val(),10)-1,parseInt($('#day_input').val(),10));
var endDate = new Date(parseInt($('#yearr_input').val(),10),parseInt($('#monthr_input').val(),10)-1,parseInt($('#dayr_input').val(),10));
var bike = $('#bike').val();
var price = calculatePrice(startDate,endDate,bike);
$('#car-price').val(price);
});
$('#cars_input').change();
});
</script>
我所做的有指定一个变量,然后我试图把变量添加到一个名为priceMatrix变量数组,但它不是以某种方式分配的。我认为这是因为我分配变量,因为它在jQuery中完成,你可以请帮我解决这个问题吗?
编辑:的jsfiddle演示 - http://jsfiddle.net/tSsvb/2/
您发布的鳕鱼似乎本身就是罚款。你必须更具体地了解什么是错的(什么不是你所期望的?),并提供更多的代码上下文。 – Cameron 2012-03-07 17:40:24
什么是“carsinput”?选择?那些自定义属性还是什么?我不知道为什么,但这段代码尖叫* for循环*给我... – nico 2012-03-07 17:42:03