2017-07-23 65 views
0

我试着这个代码,它不工作,请帮助!如何检查NaN是否与字符串不相等?

下面的代码:

var test = "3"; 

function printIsNaN(value) { 
    var bool = document.getElementById("booleanValue"); 
    if (isNaN(value) === true) { 
    bool.style.color = "green"; 
    bool.innerHTML = "true"; 
    } else if (isNaN(value) === false) { 
    bool.style.color = "red"; 
    bool.innerHTML = "false"; 
    } 
} 

printIsNaN(test); 
<!DOCTYPE html> 
<html lang="en-US"> 
<head> 
    <meta charset="utf-8"> 
    <title>Stackoverflow Question</title> 
    <script src="something.js"></script> 
</head> 
<body> 
    <!-- START OF THE DOCUMENT --> 
    <span id="booleanValue"></span> 
</body> 
</html> 

感谢,如果你能帮助,请!!!!!!!!

+3

*不等于字符串* - 你的意思是不是'typeof'字符串? –

+0

或...'Number.isNaN'? – pinkfloydx33

+0

预期结果是什么? – guest271314

回答

0

你可以使用parseInt函数()将字符串变成一个整数

function printIsNaN(value) { 
    var bool = document.getElementById("booleanValue"); 
    if (isNaN(parseInt(value))) { 
    console.log("nope"); 
    } else if (!isNaN(parseInt(value))) { 
    console.log("yep"); 
    } 
} 

希望这有助于

相关问题