所以基本上我需要添加到此表单-http://jsfiddle.net/tSsvb/自动计算价格。例如,参数是这些-(仅测试3辆自行车,可能有100甚至200个)。
所以基本上-
Bike 1 -
Price for 1 - 2 days in Season 1 - 5$ per day.
Price for 1 - 2 days in Season 2 - 10$ per day.
Price for 1 - 2 days in Season 3 - 20$ per day.
Price for 3 - 7 days in Season 1 - 4$ per day.
Price for 3 - 7 days in Season 2 - 7$ per day.
Price for 3 - 7 days in Season 3 - 15$ per day.
Price for 8+ days in Season 1 - 3$ per day.
Price for 8+ days in Season 2 - 5$ per day.
Price for 8+ days in Season 3 - 12$ per day.
Bike 2 -
Price for 1 - 2 days in Season 1 - 10$ per day.
Price for 1 - 2 days in Season 2 - 20$ per day.
Price for 1 - 2 days in Season 3 - 30$ per day.
Price for 3 - 7 days in Season 1 - 7$ per day.
Price for 3 - 7 days in Season 2 - 15$ per day.
Price for 3 - 7 days in Season 3 - 25$ per day.
Price for 8+ days in Season 1 - 5$ per day.
Price for 8+ days in Season 2 - 12$ per day.
Price for 8+ days in Season 3 - 22$ per day.
Bike 3 -
Price for 1 - 2 days in Season 1 - 3$ per day.
Price for 1 - 2 days in Season 2 - 5$ per day.
Price for 1 - 2 days in Season 3 - 10$ per day.
Price for 3 - 7 days in Season 1 - 2$ per day.
Price for 3 - 7 days in Season 2 - 3$ per day.
Price for 3 - 7 days in Season 3 - 7$ per day.
Price for 8+ days in Season 1 - 1$ per day.
Price for 8+ days in Season 2 - 2$ per day.
Price for 8+ days in Season 3 - 5$ per day.
而且季节日期是-
Season 1: 1 January to 10 June and 21 September to 31 December
Season 2: 11 June to 30 June and 1 September to 20 September
Season 3: 1 July to 31 August
因此,让我们进行测试计算。
如果我选择7月1日至9月25日期间的日期,则将针对以下第3辆自行车进行计算-
62 * 5 + 20 * 2 + 5 * 1 = 310 + 40 + 5 = 355 $
并且此总和应自动添加到文本字段“价格”中。如果我更改日期,价格也会自动更改。有没有简单的方法可以创建类似的东西?如果您有任何疑问-提出问题,我们将很乐意为您解答,因此您可以帮助我更轻松地解决此问题。
最佳答案
实时示例:http://jsfiddle.net/tSsvb/1/
我从2个变量开始
各种季节的数组
代表定价矩阵的对象
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 priceMatrix = {
bike3: {
1: { t1: 2, t2: 2, t3: 1},
2: { t1: 5, t2: 3, t3: 2},
3: { t1: 10, t2: 3, t3: 5}
}
};
第一个非常简单。第二个我只修改了bike3,因为它是您在示例中使用的那个。
1
,2
和3
代表季节,t1
-t3
代表付款级别,其中t1
硬编码为1-2天,t2
为3-7,t3
为8+。然后,我创建了2个函数。一个获得指定日期的季节:
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"
return priceMatrix[bike][season][tier] * days;
}
下一步是一种基于开始日期,结束日期和自行车进行实际计算的方法:
function calculatePrice(startDate, endDate, bike)
{
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;
}
最后一部分是将其连接起来,以便对下拉菜单的任何更改重新计算。 jQuery是您的朋友。我对应该导致重新计算的所有元素添加了
recalc
类,对所有元素都添加了id以使其更易于引用,并加入了change
事件以构建参数并调用该方法:$('.recalc').change(function(){
var startDate = new Date(parseInt($('#yd').val(),10),parseInt($('#md').val(),10)-1,parseInt($('#dd').val(),10) );
var endDate = new Date(parseInt($('#yr').val(),10),parseInt($('#mr').val(),10)-1,parseInt($('#dr').val(),10));
var bike = $('#bike').val();
var price = calculatePrice(startDate,endDate,bike);
$('#price').val(price);
});
希望有帮助,您可能需要从PHP生成定价矩阵,但这对您来说是一种锻炼:)