简体   繁体   English

我为函数的返回值得到一个空字符串

[英]I am getting a empty string for my function's return value

I am getting a empty string when I try to use my function's return value in another function. 当我尝试在另一个函数中使用函数的返回值时,我得到一个空字符串。 I declared the function value as variable so I am not sure why I am getting that. 我将函数值声明为变量,所以我不确定为什么我会这样做。

 const fnCalIn = calIn(); const fnCalOut = calOut(); //to get the value in the calIn input field and turn it into a number then print to page function calIn() { var calIn = document.getElementById("calIn").value; var clCalIn = Number(calIn); document.getElementById("demo-1").innerHTML = clCalIn; return clCalIn; }; //to get the value in the calOut input field and turn it into a number then print to page function calOut() { var calOut = document.getElementById("calOut").value; var clCalOut = Number(calOut); document.getElementById("demo-2").innerHTML = calOut; return calOut; }; // Where I want to get the difference of the calOut and calIn values and print that to the page function calDt() { var clCalDt = fnCalOut - fnCalIn; document.getElementById("calDt").innerHTML = clCalDt; } 
 <h1 class="current">Today</h1> <div> <h3>Calories in </h3> <input id="calIn"> <button onclick="calIn()">Get value</button> <p id="demo-1"></p> </div> <div> <h3>Calories Out </h3> <input id="calOut"> <button onclick="calOut()">Get value</button> <p id="demo-2"></p> </div> <div> <h3>Deficit </h3> <div> <p id="calDt"></p> <button onclick="calDt()">Get value</button> </div> </div> 

I keep getting a 0 When I change the HTML to clCalDt, I should get some value depending on what I enter in my input field but I get nothing. 我一直得到0当我将HTML更改为clCalDt时,我应该得到一些值,这取决于我在输入字段中输入的内容但我什么也得不到。 And when I try logging the values I get an empty string "". 当我尝试记录值时,我得到一个空字符串“”。

You're calling the functions (fnCalIn and fnCalOut) only once at the beginning and then their values remain static and any changes in the page don't affect them. 您只在开始时调用函数(fnCalIn和fnCalOut)一次,然后它们的值保持静态,并且页面中的任何更改都不会影响它们。 Try changing 尝试改变

var clCalDt = fnCalOut - fnCalIn;

to: 至:

var clCalDt = calOut() - calIn();

in order for the values to be re-calculated. 为了重新计算值。

Couple of issues. 几个问题。 You were returning return calOut; 你回来了返回calOut; and not return clCalOut, and in the calculation you need to call the functions. 并且不返回clCalOut,并且在计算中需要调用函数。 Might want to check to make sure then entry is actually a number somewhere. 可能要检查以确保输入实际上是某个地方的数字。

 //to get the value in the calIn input field and turn it into a number then print to page function calIn() { var calIn = document.getElementById("calIn").value; var clCalIn = Number(calIn); document.getElementById("demo-1").innerHTML = clCalIn; return clCalIn; }; //to get the value in the calOut input field and turn it into a number then print to page function calOut() { var calOut = document.getElementById("calOut").value; var clCalOut = Number(calOut); document.getElementById("demo-2").innerHTML = calOut; return clCalOut; }; // Where I want to get the difference of the calOut and calIn values and print that to the page function calDt() { var clCalDt = calOut() - calIn(); document.getElementById("calDt").innerHTML = clCalDt; } 
 <h1 class="current">Today</h1> <div> <h3>Calories in </h3> <input id="calIn"> <button onclick="calIn()">Get value</button> <p id="demo-1"></p> </div> <div> <h3>Calories Out </h3> <input id="calOut"> <button onclick="calOut()">Get value</button> <p id="demo-2"></p> </div> <div> <h3>Deficit </h3> <div> <p id="calDt"></p> <button onclick="calDt()">Get value</button> </div> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我尝试将函数的返回值与字符串连接时我的代码不起作用? - why my code doesn't work when I am trying to concatenate a function's return value with a string? 为什么我的Android应用程序上出现空字符串? - Why am I getting empty string on my android application? 为什么我的返回值中有逗号? JavaScript Celebrity Name Revealer - Why am I getting commas in my return value? JavaScript Celebrity Name Revealer 我试图获得使用return函数的附加值,但没有得到| 您能帮助我我到底在做什么错吗? - I am trying to get the addition value of using return function but not getting | Could you help to me what exactly I am doing wrong? 为什么我收到此警告 159:35 警告预计将在箭头 function 数组回调返回的末尾返回一个值? - Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return? 为什么我的元素值没有改变?我使用了错误的功能吗? - Why is my element value not getting changed? Am I using the wrong function? 我的javascript函数出现函数未定义错误 - I am getting a function undefined error with my javascript function 调用jQuery.i18n库后,我得到了Uncaught TypeError:字符串不是我的登录方法中的函数 - after calling jQuery.i18n library I am getting Uncaught TypeError: string is not a function in my login method 如果value为0,则在Angularjs中返回空字符串 - If value is 0 then return empty string in Angularjs 我试图包装带有html元素的函数的返回值 - I am trying to wrap a return value from a function with an html element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM