写在前面
下面html代码可以直接在菜鸟教程的网站去运行,比较方便快捷,省时省力.
代码功能描述
下面这个JS作用一是校验input输入框中输入的数字是否合法,二是同时在不合法时,会在input框的后面追加显示提示信息.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试JS动态改变<td>标签值</title>
<script> function func(obj,spanId){ var v = obj.value; var lessLength = v.split("-").length-1; var eLength = v.split("e").length-1; var plusLength = v.split("+").length-1; //判断输入是否合法 //检验规则是:只允许整数,正整数,负整数,以及科学计数法 if(v="" || isNaN(v) || (v.indexOf(".")>0) || lessLength > 1 || eLength > 1 || plusLength > 1 || (v.indexOf("-") > 0) || (v.indexOf("+") > 0) || v == "" ){ document.getElementById(spanId).innerHTML = "输入不合法!"; }else{ document.getElementById(spanId).innerHTML = "GOOD!"; } } </script>
</head>
<body>
<p>下面是一个一行两列的table</p>
<br>
<table border="1">
<tr>
<td>后面是type=name的input输入框</td>
<td><input type="number" id="qqq" onchange="func(this,'sss');"><span id="sss" style="color:red;">输入合法吗?</span></td>
</tr>
</table>
<br>
<p>在输入内容发生变化时会动态的检验是否是数字</p>
<br>
<a href="http://blog.csdn.net/qq1332479771">我的博客</a>
</body>
</html>