为什么计算代码不起作用?

时间:2012-03-07 17:36:31

标签: javascript

所以基本上,请检查此代码 -

完整代码 -

  <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/

3 个答案:

答案 0 :(得分:1)

很难说没有看到你的HTML。但是,我觉得它与你试图摆脱标签的内容有关:

($('#cars_input option[value="'+cars+'"]').attr('priceone'));

以上将获得属性“priceone”的值。例如,您需要将其构建为使用$12.00来获取.attr('priceone')

<option value="car-x" priceone="$12.00"> Car-X </option>
  • 如果您想要该选项的值,请使用.val()
  • 如果您想要ATTRIBUTE“priceone”的值,请使用.attr('priceone')
  • 如果您想要选项标签中的文字,请使用.text()

我希望这至少能指出你正确的方向!

同样,你也应该在这些问题中发布你的HTML,以便我们更容易看到全局。

答案 1 :(得分:0)

当您从DOM获取数据时,它们不是数字而是字符串。您必须使用parseInt()parseFloat()

强制使用数字类型

答案 2 :(得分:0)

.attr('priceone'));Priceone不是属性。 您应该调用该属性。假设它是ID你应该这样做:.attr("id")。返回字符串。