javascript - 当获得两个不同函数中的两个变量的总数时,结果为 NaN
问题描述
为什么在访问 2 个不同函数的变量时会得到 NaN 结果?
我的代码
var n_standard = 0;
var n_quad = 0;
var totalQuad;
var totalStandard;
var total = totalStandard + totalQuad;
function standardRoom(){
n_standard = n_standard + Number($("#selectBoxStandard").val());
var xSumStandard = Number($("#n_adultStandard").val()) + Number($("#n_childrenStandard").val());
totalStandard = {{$availableRooms[0]['nightRate'] * $n_nights }} * n_standard;
if ($('select#selectBoxStandard option').length > 1 && Number($("#selectBoxStandard").val()) !== 0) {
$('#selectBoxStandard').find("option:nth-last-child(-n+" + $('#selectBoxStandard').val() + ")").remove();
$("#roomDetailStandard ul").html('<li><strong>Standard Room - Regular Online Rate</strong> <strong><a href="javascript:removeStandard()" class="pull-right" style="color:red"><u>remove</u></a></strong></li>'+
'<li class="pull-right"><h4 style="color:darkorange">PHP ' + totalStandard + '</h4></li>'+
'<li>Number of night(s): {{$n_nights}} </li>'+
'<li>Number of person(s): ' + xSumStandard + '</li>'+
'<li class="hr">Number of room(s): '+ n_standard +'</li><hr />'
);
$("#totals ul").html('<li>Total:'+ total +'</li>');
return totalStandard;
}else {
alert("Select No. of rooms");
}
}
function quadRoom(){
n_quad = n_quad + Number($("#selectBoxQuad").val());
var xSumQuad = Number($("#n_adultQuad").val()) + Number($("#n_childrenQuad").val());
totalQuad = {{$availableRooms[1]['nightRate'] * $n_nights}} * n_quad;
if ($('select#selectBoxQuad option').length > 1 && Number($("#selectBoxQuad").val()) !== 0) {
$('#selectBoxQuad').find("option:nth-last-child(-n+" + $('#selectBoxQuad').val() + ")").remove();
$("#roomDetailQuad ul").html('<li><strong>Quad Room - Regular Online Rate</strong> <strong><a href="javascript:removeQuad()" class="pull-right" style="color:red"><u>remove</u></a></strong></li>'+
'<li class="pull-right"><h4 style="color:darkorange">PHP ' + totalQuad + '</h4></li>'+
'<li>Number of night(s): {{$n_nights}} </li>').append('<li>Number of person(s): ' + xSumQuad + '</li>'+
'<li class="hr">Number of room(s): '+ n_quad +'</li><hr />'
);
$("#totals ul").html('<li>Total:'+ total +'</li>');
return totalQuad;
}else {
alert("Select No. of rooms");
}
}
<div class="button-style-1" style="padding-bottom:80px" style="padding-bottom:40px"><a href="javascript:standardRoom()" ><i class="fa fa-calendar"></i><span class="mobile-visibility">BOOK</span></a></div>
<div class="button-style-1" style="padding-bottom:80px" style="padding-bottom:40px"><a href="javascript:quadRoom()" ><i class="fa fa-calendar"></i><span class="mobile-visibility">BOOK</span></a></div>
我试图返回totalStandard和totalQuad然后将它们加在一起total,然后将它们输出回函数中。我在这里遗漏了一些东西,我是 javascript 新手,所以我需要一些帮助
解决方案
n_standard = n_standard + Number($("#selectBoxStandard").val());
var xSumStandard = Number($("#n_adultStandard").val()) +
Number($("#n_childrenStandard").val());
totalStandard = {{$availableRooms[0]['nightRate'] * $n_nights }} * n_standard;
这段代码和你计算 totalQuad 的地方应该被数字检查函数包围,以确保变量是数字。例如,为了确保每次只取数字,您可以检查if($.isNumeric($availableRooms[0]['nightRate'])){.......}
. 如果任何变量不是数字,则结果将为 NaN。所以请在数学运算之前检查每个数字变量。编辑:var totalQuad;
var totalStandard;
var total = totalStandard + totalQuad; totalStandard=9;
console.log("result="+total);
这将导致 NaN,因为我没有在这里定义 totalQuad 变量。如果首先将两个变量都初始化为 0,那将起作用。
第二次编辑:
var totalQuad;
var totalStandard;
var total = totalStandard + totalQuad;
function standardRoom(){
totalStandard=9;
}
function quadRoom(){
totalQuad=5;
}
function Total()
{
console.log(totalStandard);
console.log(totalQuad);
console.log(total); // THIS will RESULT IN NaN, as WHEN Total variable initialized, totalStandard and totalQuad are not initialized then. Latter initialization doesn't reinitialize total value
console.log("Total="+(totalStandard*totalQuad));
}
<div class="button-style-1" style="padding-bottom:80px" style="padding-bottom:40px"><a href="javascript:standardRoom()" ><i class="fa fa-calendar"></i><span class="mobile-visibility">BOOK</span></a></div>
<div class="button-style-1" style="padding-bottom:80px" style="padding-bottom:40px"><a href="javascript:quadRoom()" ><i class="fa fa-calendar"></i><span class="mobile-visibility">BOOK</span></a></div>
<div class="button-style-1" style="padding-bottom:80px" style="padding-bottom:40px"><a href="javascript:Total()" ><i class="fa fa-calendar"></i><span class="mobile-visibility">Total</span></a></div>
推荐阅读
- c - 为什么系统调用失败时c程序不向stderr打印错误消息?
- hibernate - 不正确的长类型
- sql - Postgres:只处理一次增长表的每条记录
- android - 如何使用 API 创建自定义应用电报?
- powershell - dotnet 命令失败后如何停止 powershell 脚本?
- python-3.x - 如何获得具有变量和约束的所有变体/组合?蟒蛇 3.6
- matlab - 在 Matlab 的直方图中拟合分布线
- java - 给定 R(x,y) 形式的断言列表,检查 2 条狗 x 和 y 是否具有相同品种的算法
- javascript - 单击时隐藏/显示具有相同类的元素
- java - Hibernate 抱怨 @OneToOne 中的 @Id 为空,即使它不为空