<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
 </head>
 <body>
<input type="text" name="zb" id="aa"> <input type="text" name="yb" id="bb"> <script>
var aa = document.getElementById("aa");
var bb = document.getElementById("bb");

 function gg(){
if(isNaN(aa.value)){
alert("不是数字");
aa.value = "";
aa.focus();
}else if(aa.value!=""){
alert("是数字");
return false;
}
}  function ff(){
if(isNaN(bb.value)){
alert("不是数字");
bb.value = "";
bb.focus();
}else if(bb.value!=""){
alert("是数字");
return false;
}
} aa.onblur = gg; bb.onblur = ff;

</script>
</body>
</html>
gg()和ff() 两个函数 功能基本相同,  我想改成函数传参的方式  只保留其中一个  应该如何去做呢? 请问

解决方案 »

  1.   


    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
      <title>Document</title>
     </head>
     <body>
    <input type="text" name="zb" id="aa" onblur="gg(this)"><input type="text" name="yb" id="bb" onblur="gg(this)"><script>
     function gg(_this){
    if(isNaN(_this.value)){
    alert("不是数字");
    _this.value = "";
    _this.focus();
    }else if(_this.value!=""){
    alert("是数字");
    return false;
    }
    }</script>
    </body>
    </html>
      

  2.   

    能不能不动html的 
      

  3.   


            var aa = document.getElementById("aa");
            var bb = document.getElementById("bb");
            function ff() {
                if (isNaN(this.value)) {
                    alert("不是数字");
                    this.value = "";
                    this.focus();
                } else if (this.value != "") {
                    alert("是数字");
                    return false;
                }
            }        aa.onblur =  bb.onblur = ff;Web开发学习资料推荐
    jqGrid colModel配置参数
    easyui datebox设置日期范围
      

  4.   


    [].forEach.call(document.querySelectorAll("#aa,#bb"),function($dom){
      $dom.onblur = function(){
        if (isNaN(this.value)) {
            alert("不是数字");
            this.value = "";
            this.focus();
          } else if (this.value != "") {
            alert("是数字");
            return false;
          }
        }
    });