我正在使用谷歌地图等使用距离计算器脚本检索距离...
然后根据该距离计算车费,但该脚本的输出在距离字段中从 Google 动态显示为 x,y 英里
使用 document.getElementById("distance_road").value = distance1;
和 distance1 = response.routes[0].legs[0].distance.text;
在“成本”字段中接受并以这种格式 (00.00) 输入数据,因此在尝试运行计算时,输出结果为 $NAN
。
这是成本脚本:
function calculatefare() { /* variable declarations*/
var subTotal=0.00;
var pricePerFifthMile=0.54;
var dropOffCharge=2.60;
var overTwoPassengerCharge=0.50;
var tripMilage=Number(document.getElementById("distance_road").value);
var passengers=Number(document.getElementById("passengers").value);
/* if there are over 2 riders, each additional passenger pays $2 if (passengers>2)*/ { subTotal=overTwoPassengerCharge*(passengers-3+1); }
/* calculate the price based on miles in*/
subTotal+=parseInt(tripMilage*5)*pricePerFifthMile; subTotal+=dropOffCharge; /* prints the price to the div. toFixed adds cents to integer calculations */
document.getElementById('price').innerHTML=" $"+subTotal.toFixed(2);
}
// JavaScript Document
有没有办法让这个脚本接受那种格式(阅读它)(xx,x 英里)
或者假设将此输出清理为 xx.x,这意味着删除“,”和“英里”。
我试过了:
sanitizedDistance = parseFloat(distance1.replace(",","."));
最佳答案
像这样简单地链接英里和逗号替换
sanitizedDistance = parseFloat(distance1.replace("miles","").replace(",","."))
关于javascript - 使用 parseFloat Sanitize 或其他方法清理变量输出值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331987/