"undefined" 错误在 Javascript 中打印出来
"undefined" error prints out in Javascript
美好的一天,
我正在根据以下内容编写一个简单的计算价格:
单一价格
该价格的频率(每周、每天等)。
我一直在页面上打印出“未定义”一词。我试图重新定义该部分的所有变量。所有变量均已正确定义。你们看到我下面的代码有什么错误吗?
//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>
文档中已经定义了以下变量:
//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);
谢谢!
编辑:这是我第一次在这里发帖。感谢您的耐心等待以及您对如何使我的问题更容易理解的评论!
我做了一些扩展研究并发现了我的错误:
我忘了在最后一个 IF 语句中添加 else。
if(totalParkingCost1>0){
var printOutTotalParkingCost1 = 'XYZ';
}
在任何情况下都使用了 var printOutTotalParkingCost1。我添加了以下 else
else{
var printOutTotalParkingCost1 = "";
}
现在,无论场景如何,变量 printOutTotalParkingCost1 都已定义。谢谢大家的热心帮助!感谢@ivar 的详尽解释。
美好的一天, 我正在根据以下内容编写一个简单的计算价格: 单一价格 该价格的频率(每周、每天等)。
我一直在页面上打印出“未定义”一词。我试图重新定义该部分的所有变量。所有变量均已正确定义。你们看到我下面的代码有什么错误吗?
//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>
文档中已经定义了以下变量:
//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);
谢谢!
编辑:这是我第一次在这里发帖。感谢您的耐心等待以及您对如何使我的问题更容易理解的评论!
我做了一些扩展研究并发现了我的错误: 我忘了在最后一个 IF 语句中添加 else。
if(totalParkingCost1>0){
var printOutTotalParkingCost1 = 'XYZ';
}
在任何情况下都使用了 var printOutTotalParkingCost1。我添加了以下 else
else{
var printOutTotalParkingCost1 = "";
}
现在,无论场景如何,变量 printOutTotalParkingCost1 都已定义。谢谢大家的热心帮助!感谢@ivar 的详尽解释。