[英]"undefined" error prints out in Javascript
Good day, I'm coding a simple calculation price based on: A single price The frequency of that price (Weekly, daily, etc.).美好的一天,我正在编写一个简单的计算价格基于: 单一价格 该价格的频率(每周、每天等)。
I keep having the word "undefined" being printed out in the page.我一直在页面上打印出“未定义”这个词。 I've tried to redefine all the variable of that part.我试图重新定义该部分的所有变量。 All variables are correctly defined.所有变量都被正确定义。 Do you guys see any error in my code below?你们在下面的代码中看到任何错误吗?
//parking cost var parking_cost1 = 0; var parkingCostUnity1 = 0; var parkingCostFrequency1 = 0; parking_cost1 = new Number(document.getElementById("parking_cost1").value); parkingCostFrequency1 = document.getElementById("parking_cost1_frequence1").value; if (parkingCostFrequency1 == "per_night") { parkingCostUnity1 = result; } if (parkingCostFrequency1 == "per_week") { parkingCostUnity1 = nrOfWeeks; } if (parkingCostFrequency1 == "per_month") { parkingCostUnity1 = nrOfMonths; } var echoParkingCostFrequency1 = parkingCostFrequency1.replace("_", " "); var totalParkingCost1 = 0; totalParkingCost1 = parkingCostUnity1 * parking_cost1; if (totalParkingCost1 > 0) { var printOutTotalParkingCost1 = '<tr><td><b>Total parking cost</b></td><td>' + parking_cost1 + ' ' + currency + ' ' + echoParkingCostFrequency1 + '</td><td>' + totalParkingCost1 + ' ' + currency + '</td></tr>'; }
<i class="fas fa-parking"></i> <text class="form_basic_text" />Parking cost<br> <input type="number" onChange="resume()" name="parking_cost1" id="parking_cost1" class="form_basic_field_bid" style="width:auto;" /><br/> <center> <select onChange="resume()" name="parking_cost1_frequence1" id="parking_cost1_frequence1" style="width:auto;height:25px;padding:0px;width:auto;"> <option value="per_night">Per night</option> <option value="per_week">Per week</option> <option value="per_month">Per month</option> </select> </center>
The following variables have already been defined in the document:文档中已经定义了以下变量:
//var currency
var currency = document.getElementById("currency").value;
//This variable is always defined, as it's a dropdown.
//var result, nrOfWeeks and nrOfMonths are all under the same parameters
//check-in date
var checkInDate = new Date(document.getElementById("check_in_date").value);
var checkInDateResume = checkInDate.getDate() + '-' + (checkInDate.getMonth()+1) + '-' + checkInDate.getFullYear();
if(checkInDateResume=="NaN-NaN-NaN"){
checkInDateResume = "<text class='alert'>Not defined yet</text>";
result = 1;
nrOfWeeks = 1;
nrBiWeeks = 1;
nrOfMonths = 1;
}
var checkOutDate = new Date(document.getElementById("check_out_date").value);
var checkOutDateResume = checkOutDate.getDate() + '-' + (checkOutDate.getMonth()+1) + '-' + checkOutDate.getFullYear();
if(checkOutDateResume=="NaN-NaN-NaN"){
checkOutDateResume = "<text class='alert'>Not defined yet</text>";
result = 1;
nrOfWeeks = 1;
nrBiWeeks = 1;
nrOfMonths = 1;
}
//Since the nr of months, week and result (the number of nights) is the difference between the check-in and check-out date - Those aren't defined except as mentionned up here. We then define the rest of the vars
var difference = Math.abs(checkOutDate-checkInDate);
var result = difference/(1000*3600*24);
var nrOfMonths = Math.ceil(result/30.41);
var nrOfWeeks = Math.ceil(result/7);
Thank you!谢谢!
Edit: It's my first time posting here.编辑:这是我第一次在这里发帖。 Thank you for your patience and your comments on how to make my question more understandable!感谢您的耐心和您对如何使我的问题更容易理解的评论!
I did some extended research and discovered my mistake: I forgot to add an else to the the last IF statement.我做了一些扩展研究并发现了我的错误:我忘记在最后一个 IF 语句中添加 else。
if(totalParkingCost1>0){
var printOutTotalParkingCost1 = 'XYZ';
}
The var printOutTotalParkingCost1 was used in any scenario. var printOutTotalParkingCost1 用于任何场景。 I added the following else我添加了以下其他内容
else{
var printOutTotalParkingCost1 = "";
}
Now, the variable printOutTotalParkingCost1 is defined whatever the scenario is.现在,无论场景如何,都定义了变量 printOutTotalParkingCost1。 Thank you all for your kind help!谢谢大家的热心帮助! Thanks to @ivar for his extensive explanations.感谢@ivar 的详尽解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.