我正在創建一個web應用程序來幫助像我這樣的糖尿病患者。我遇到了麻煩,它會自動將我的胰島素設置爲碳水化合物比例{myFunction()}。它只是不會出現。該代碼是...Javascript不能用於計算
<HTML>
<head>
<meta name="viewport" content="initial-scale=2, user-scalable=no">
<meta name= apple-mobile-web-app-capable content= yes />
<meta name= apple-mobile-web-app-status-bar-style content= black />
<link rel="apple-touch-icon" href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-SD-Iphone.png" />
<link rel="apple-touch-icon" sizes= 114x114 href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-HD-Iphone.png" />
<link rel="apple-touch-icon" sizes= 72x72 href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-SD-Ipad.png" />
<link rel="apple-touch-icon" sizes= 144x144 href="https://dl.dropboxusercontent.com/u/11681462/untitled%20folder/Icon/Icon-HD-Ipad.png" />
<Title> Diabetes Cal </Title>
<script>
function valuechanged() {
var a = document.getElementById('textA').value;
var b = document.getElementById('textB').value;
var t = document.getElementById('textX').value;
var y = document.getElementById('textY').value;
var z = document.getElementById('textZ').value;
var dec = t * 0.1;
var x = parseFloat(dec).toFixed(1);
if (+x - +y < 0) {
var c = 0;
}
else
{
c = +x - +y;
}
if (c < 0) {
var l = 0;
}
else
{
var l = c/z;
}
var d = parseFloat(l).toFixed(2);
if (a/b < 0)
{
var u = 0;
}
else
{
var u = a/b;
}
var e = parseFloat(u).toFixed(2);
document.getElementById('labelS').innerHTML = e;
document.getElementById('labelG').innerHTML = d;
document.getElementById('labelX').innerHTML = +x;
document.getElementById('labelA').innerHTML = a;
document.getElementById('labelJ').innerHTML = +d + +e;
}
function myFunction(){
var TS=document.getElementById('textB')
var date= new Date();
var time = date.getHours();
if (time<10)
{
CR = "8";
}
else if (time<16)
{
CR = "10";
}
else if (time<20)
{
CR = "5";
}
else
{
CR = "8";
}
TS.value= CR
}
</script>
</head>
<body BGColor=orange onload="valuechanged();myFunction();" > <center><br> Bolus Wizard <br>
<div>
Bg (without Decimal)<br>
<input type="text" pattern=\d* id="textX" min="25" max="333" value="25" onchange="valuechanged();" /><br><label ID="labelX">-----</label> mmol/L<br>
</div><hr width=45% color=black size=0.5>
<div>
Carbs<br> <input type="text" pattern=\d* ID="textA" max=300 value="0" onchange="valuechanged();" /><br>
</div><hr width=45% color=black size=0.5>
<div>
Carb Ratio<br><input type="text" pattern=\d* ID="textB" value="" onchange="valuechanged();" />
</div>
<div>
Target<br><input type="text" pattern=\d* ID="textY" value="6" onchange="valuechanged();" />
</div>
<div>
Correction Factor<br><input type="text" pattern=\d* ID="textZ" value="2" onchange="valuechanged();" />
</div>
<div>
Food<br> <label ID="labelS"> ---</label> Units
</div>
<p>
<div>
Correction<br> <label ID="labelG"> ---</label> Units
</div>
<p>
<div>
You Need...<br> <label ID="labelJ"> ---</label> Units
</div>
<p>
</center>
</body>
</HTML>
我希望有人能調試我problem.t是混亂的。其計算公式爲 (Blood Sugar{x}-Target{y} -:- (Correction Factor{z})+ (carbs{a} -:- Carb ratio{b})
這是工作之前,我加入了小數位
var dec = t * 0.1; var x = parseFloat(dec).toFixed(1);
此外,如果任何人都可以教我如何爲
創建設置TextZ,textY處和TEXTB
在不同的頁面上會很棒的區域。下午我(如果你能在這裏)
感謝洙多
BTW,一元'+'時,才需要作爲保障爲另外,其他mathetmatic運營商將自動字符串轉換爲數字(例如'C = + x - + y'可以是'c = x - y'。您應該測試輸入值以確保它們在使用前適當地轉換爲數字(這與您如何將它們轉換爲數字無關) – RobG
如果我這樣做它給了我NaN –
它不應該(也不適用於我),你一定錯了別的東西 – RobG