JS 判断是否是数字

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input id="demo" type="text">
<script>
function myFunction(){
	var x = document.getElementById("demo").value;
	if(x == "" || isNaN(x)){
		console.info(x+" :不是数字");
	}else{
		console.info(x+" :是数字");
	}
}
</script>
<button type="button" onclick="myFunction()">点击这里</button>
</body>
</html>

效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2327486