[英]NaN Custom Error Message in Javascript
我嘗試了在另一個線程中顯示的isNaN(x)
方法,但是嘗試它不會返回任何內容。
這是我的代碼:
<html>
<head>
<meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="NKit.js"></script>
<meta name = "apple-mobile-web-app-capable" content = "yes" />
<style>
body
{
background-color:LightGray;
}
h1
{
color:Navy;
text-align:center;
font-family:"Helvetica Neue";
}
h3
{
color:Navy;
text-align:center;
font-family:"Helvetica Neue";
}
h5
{
color:CornflowerBlue;
text-align:center;
font-family:"Helvetica Neue";
}
p
{
font-family:"Helvetica Neue";
font-size:20px;
text-align:center;
color:CadetBlue;
}
</style>
</head>
<body>
<center>
<h1> The Snotor Test Calc </h1>
<h3> Enter two numbers below. The blank field will be worked out for you. The algorithm is: 'a + b = c'; Therefore 'c - a = b', and 'c - b = a'. </h3>
<button type="button" onclick="funca()">A</button> <br> <input id="ai" type="text"> <br> <br>
<button type="button" onclick="funcb()">B</button> <br> <input id="bi" type="text"> <br> <br>
<button type="button" onclick="funcc()">C</button> <br> <input id="ci" type="text"> <br> <br>
<script>
function funca()
{
//Get the value of input fields
var av = document.getElementById("ai");
var bv = document.getElementById("bi").value;
var cv = document.getElementById("ci").value;
if (isNaN(parseFloat(cv))==true||isNaN(parseFloat(bv))==true){
av.value = cv-bv;
else {
av.value="Input Error!";
}
</script>
<script>
function funcb()
{
//Get the value of input fields
var av = document.getElementById("ai").value;
var bv = document.getElementById("bi");
var cv = document.getElementById("ci").value;
bv.value = parseFloat(cv)-parseFloat(av);
}
</script>
<script>
function funcc()
{
//Get the value of input fields
var av = document.getElementById("ai").value;
var bv = document.getElementById("bi").value;
var cv = document.getElementById("ci");
cv.value = parseFloat(av)+parseFloat(bv);
}
</script>
<h1> Logo Here... </h1>
</body>
</html>
基本上,設計按鈕后的Javascript,以便在其他輸入之一無效時,輸入框不會被NaN
填充。
注意:此操作僅在按鈕'a'的funca
上完成。 無論輸入“ b”和“ c”是否無效,“ a”根本不起作用。 但是'b'和'c'可以完美工作(如果其他輸入之一出錯,則顯示NaN
)。
插入自定義NaN錯誤后,此錯誤將被放入PhoneGap中並發送給Apple。
注意:然后將算法更改為財務算法...
您說的是數字不是數字時,您將對其進行計算。 檢查此更新的javascript。
看到這個小提琴: http : //jsfiddle.net/N2mm4/2/
這是更新的javascript:
function funca()
{
//Get the value of input fields
var av = document.getElementById("ai");
var bv = parseFloat(document.getElementById("bi").value);
var cv = parseFloat(document.getElementById("ci").value);
if (!isNaN(cv) && !isNaN(bv)){
ai.value = cv-bv;
}
else {
ai.value="Input Error!";
}
}
function funcb()
{
//Get the value of input fields
var av = document.getElementById("ai").value;
var bv = document.getElementById("bi");
var cv = document.getElementById("ci").value;
bv.value = parseFloat(cv)-parseFloat(av);
}
function funcc()
{
//Get the value of input fields
var av = document.getElementById("ai").value;
var bv = document.getElementById("bi").value;
var cv = document.getElementById("ci");
cv.value = parseFloat(av)+parseFloat(bv);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.