如果值大于另一个值,jQuery将在文本框中回显语句

时间:2014-05-17 10:04:24

标签: jquery if-statement echo

我有一个代码,用于计算A点和B点的里程数。如果距里程A或点B小于75英里,我想返回" FREE"但是,如果距离两者都超过75英里,我想要退回声明"您需要支付费用"。

我以为我可以使用简单的if,else if和else语句处理这个问题,但它根本不起作用:

function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
}
var directionsService = new google.maps.DirectionsService();
var directionsService2 = new google.maps.DirectionsService();

function calcRoute() {
var distance1Input = document.getElementById("distance1");
var MileageInput = document.getElementById("mileage");
var start = "DE75 7AH";

var end = document.getElementById("end").value;
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
distance1Input.value = Math.round(response.routes[0].legs[0].distance.value / 1609.344);
    } 
});

//second route
var distance2Input = document.getElementById("distance2");
var start2 = "AL8 6RE";

var end = document.getElementById("end").value;
var request2 = {
origin:start2,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService2.route(request2, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
distance2Input.value = Math.round(response.routes[0].legs[0].distance.value / 1609.344);
    } 
});

//mileage cost
var mileageCost = Math.round(((response.routes[0].legs[0].distance.value / 1609.344)-75)+100);

var derbyDistance = distance1Input.value;
var londonDistance = distance2Input.value;

if (derbyDistance < 75) {
MileageInput.value = 'FREE';
}
else if (londonDistance < 75) {
MileageInput.value = 'FREE';
}
else {
MileageInput.value = 'You will be charged expenses';
}

}

有问题的代码部分位于底部,我试图根据终点的距离返回语句。

您可以在此处查看此脚本: http://www.samskirrow.com/projects/distr_maps/index2.html

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

由于您的计算似乎正常并且文本输入似乎正确更新,我认为这可能是上一个if语句中的问题。您正在将整数(75)与value的文本输入的string进行比较。

尝试使用parseInt()来避免这些问题:

if (parseInt(derbyDistance) < 75) {
  MileageInput.value = 'FREE';
}
else if (parseInt(londonDistance) < 75) {
  MileageInput.value = 'FREE';
}
else {
  MileageInput.value = 'You will be charged expenses';
}

答案 1 :(得分:1)

var mileageCost = Math.round(((response.routes[0].legs[0].distance.value / 1609.344)-75)+100);

在浏览器控制台中,我收到response is undefined错误。

由于

directionsService2.route(request2, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
distance2Input.value = Math.round(response.routes[0].legs[0].distance.value / 1609.344);
        } 
    });

//mileage cost
var mileageCost = Math.round(((response.routes[0].legs[0].distance.value / 1609.344)-75)+100);

您正尝试访问route()函数之外的响应

答案 2 :(得分:1)

响应在.route方法调用之外是未知的。你应该通过这种方式传递它:

function initialize() {
 directionsDisplay = new google.maps.DirectionsRenderer();
}

var directionsService = new google.maps.DirectionsService();
var directionsService2 = new google.maps.DirectionsService();


function calcRoute() {
  var distance1Input = document.getElementById("distance1");
  var MileageInput = document.getElementById("mileage");

  var start = "DE75 7AH";
  var end = document.getElementById("end").value;

  var request = {
  origin:start,
  destination:end,
  travelMode: google.maps.DirectionsTravelMode.DRIVING
  };

  directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {

  directionsDisplay.setDirections(response);
  distance1Input.value = Math.round(response.routes[0].legs[0].distance.value / 1609.344);
  displayExpenses(response);
          } 
      });

  //second route
  var distance2Input = document.getElementById("distance2");
  var start2 = "AL8 6RE";

  var end = document.getElementById("end").value;
  var request2 = {
  origin:start2,
  destination:end,
  travelMode: google.maps.DirectionsTravelMode.DRIVING
  };

  directionsService2.route(request2, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay.setDirections(response);
  distance2Input.value = Math.round(response.routes[0].legs[0].distance.value / 1609.344);
  displayExpenses(response);
          } 
      });

  function displayExpenses(response){
    var mileageCost = Math.round(((response.routes[0].legs[0].distance.value / 1609.344)-75)+100);

    var derbyDistance = distance1Input.value;
    var londonDistance = distance2Input.value;

    if (derbyDistance < 75) {
      MileageInput.value = 'FREE';
    }
    else if (londonDistance < 75) {
      MileageInput.value = 'FREE';
    }
    else {
        MileageInput.value = 'You will be charged expenses';
    }
  }   
}

WorkingFiddle