<html>
<head>
<script type="text/javascript">
function check()
{
  isNone("username","用户名有能为空");
  isNone("postcode","邮编不能为空");
  isNone("handset","手机不能为空"); 
}

function isNone(txtID,tips)
{
   var txt = document.getElementById(txtID);
   if (txt.value == "") 
   {
     window.alert(tips);
     document.getElementById("soform").action="#";
   }
}
</script>
</head>
<body>
<form id="soform" name="soform" method="post" action="Count.asp" onsubmit="check();">
<input type="text" id="username" name="username" />用户名 <br />
<input type="text" id="postcode" name="postcode" />邮编 <br />
<input type="text" id="handset" name="handset" />手机 <br />

<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form> 
</body>
</html>

解决方案 »

  1.   

    把 form 的 action 设为空呢 ???
      

  2.   

    不过很奇怪,为什么不能用return ?
      

  3.   

    好多页面都要用到表单检测函数,有return太麻烦
      

  4.   

    You can cancel the Event<html> 
    <head> 
    <script type="text/javascript"> 
    function check() 

      isNone("username","用户名有能为空"); 
      isNone("postcode","邮编不能为空"); 
      isNone("handset","手机不能为空"); 
    } function isNone(txtID,tips) 

      var txt = document.getElementById(txtID); 
      if (txt.value == "") 
      { 
        window.alert(tips); 
        event.returnValue = false; 
        event.cancel = true;
      } 

    </script> 
    </head> 
    <body> 
    <form id="soform" name="soform" method="post" action="Count.asp" onsubmit="check();"> 
    <input type="text" id="username" name="username" />用户名 <br /> 
    <input type="text" id="postcode" name="postcode" />邮编 <br /> 
    <input type="text" id="handset" name="handset" />手机 <br /> <input type="submit" value="提交" /> 
    <input type="reset" value="重置" /> 
    </form> 
    </body> 
    </html>
      

  5.   

    document.all.forms[0].setAttribute("action","");
    document.getElementById("soform").setAttribute("action","");
      

  6.   

    8楼的解决了问题,form己不会提交。但是,1 当用户名,邮编,手机,全部为空时,却一个一个的进行了alert,应该用户名alert之后即停止执行2 不兼容Firefox (这个问题不算大)
      

  7.   

    function isNone(txtID,tips) 

      var txt = document.getElementById(txtID); 
      if (txt.value == "") 
      { 
        window.alert(tips);
        isNone = function(){};
        event.returnValue = false; 
        event.cancel = true;
      } 

    这样呢 
      

  8.   

    其实你得设计和抛出异常有点相似<html> 
    <head> 
    <script type="text/javascript"> 
    function check() 

      try
      {
       isNone("username","用户名有能为空"); 
       isNone("postcode","邮编不能为空"); 
       isNone("handset","手机不能为空");
      }
      catch(err)
      {
       event.returnValue = false; 
       event.cancel = true;  
      }
    } function isNone(txtID,tips) 

      var txt = document.getElementById(txtID); 
      if (txt.value == "") 
      { 
        window.alert(tips); 
        throw("Error Happens!");
      } 

    </script> 
    </head> 
    <body> 
    <form id="soform" name="soform" method="post" action="Count.asp" onsubmit="check();"> 
    <input type="text" id="username" name="username" />用户名 <br /> 
    <input type="text" id="postcode" name="postcode" />邮编 <br /> 
    <input type="text" id="handset" name="handset" />手机 <br /> <input type="submit" value="提交" /> 
    <input type="reset" value="重置" /> 
    </form> 
    </body> 
    </html>
      

  9.   

    改onsubmit="return check()"
    isNone用13楼的throw("")
    function check() 
    {
    try{
    ……
    return true;
    }
    catch(e)
    {
    return false
    }
    }
      

  10.   

    13楼高手,请问如何把try catch语句放在isNone函数里,而不是check函数?
      

  11.   

    <html> 
    <head> 
    <script type="text/javascript"> 
    function check() 

      var flag = 1;
      isNone("username","用户名有能为空"); 
      isNone("postcode","邮编不能为空"); 
      isNone("handset","手机不能为空"); 
    } function isNone(txtID,tips) 

      var txt = document.getElementById(txtID); 
      if (txt.value == "" && flag) 
      { 
        window.alert(tips); 
        event.returnValue = false; 
        event.cancel = true;
        flag = 0;
      } 

    </script> 
    </head> 
    <body> 
    <form id="soform" name="soform" method="post" action="Count.asp" onsubmit="check();"> 
    <input type="text" id="username" name="username" />用户名 <br /> 
    <input type="text" id="postcode" name="postcode" />邮编 <br /> 
    <input type="text" id="handset" name="handset" />手机 <br /> <input type="submit" value="提交" /> 
    <input type="reset" value="重置" /> 
    </form> 
    </body> 
    </html>
      

  12.   

    <html> 
    <head> 
    <script type="text/javascript"> 
    var flag=1;
    function check() 

      isNone("username","用户名有能为空"); 
      isNone("postcode","邮编不能为空"); 
      isNone("handset","手机不能为空"); 
    } function isNone(txtID,tips) 

      var txt = document.getElementById(txtID);
      if(flag==1) {
      if (txt.value == "") 
      { flag=0;
        window.alert(tips); 
        event.returnValue = false; 
        event.cancel = true;
      } 
      }

    </script> 
    </head> 
    <body> 
    <form id="soform" name="soform" method="post" action="Count.asp" onsubmit="check();"> 
    <input type="text" id="username" name="username" />用户名 <br /> 
    <input type="text" id="postcode" name="postcode" />邮编 <br /> 
    <input type="text" id="handset" name="handset" />手机 <br /> <input type="submit" value="提交" /> 
    <input type="reset" value="重置" /> 
    </form> 
    </body> 
    </html>
      

  13.   

    写个简码你没认真看 加了return 更方便又不用考虑兼容问题 又是第一时间跳出
    <html> 
    <head> 
    <script type="text/javascript"> 
    function check() 
    {
      try
      {
        isNone("username","用户名有能为空"); 
        isNone("postcode","邮编不能为空"); 
        isNone("handset","手机不能为空");
        return true;
      }
      catch(err)
      {
        return false;
      }
    } function isNone(txtID,tips) 

      var txt = document.getElementById(txtID); 
      if (txt.value == "") 
      { 
        alert(tips)
        throw("Error Happens!");
      } 

    </script> 
    </head> 
    <body> 
    <form id="soform" name="soform" method="post" action="Count.asp" onsubmit="return check();"> 
    <input type="text" id="username" name="username" />用户名 <br /> 
    <input type="text" id="postcode" name="postcode" />邮编 <br /> 
    <input type="text" id="handset" name="handset" />手机 <br /> <input type="submit" value="提交" /> 
    <input type="reset" value="重置" /> 
    </form> 
    </body> 
    </html>
      

  14.   

    建议楼主还是用上return,呵呵,15楼和24四楼的已经不错了 ...
      

  15.   

    *************************************************************************** 思想决定行动,行动决定习惯,习惯决定命运. 
    程序员在深圳QQ群,交流产生思想碰撞. 部份专业群: 
    程序员在深圳c++群15195967 
    程序员在深圳英语学习群:23864353 
    程序员在深圳c++Ⅱ17409451 
    程序员在深圳嵌入式开发群37489763 
    程序员在深圳移动开发群31501597 
    程序员在深圳创业群33653422 部份高级程序员群: 
    高级群I:17538442 
    高级群II:7120862 部份初、中级程序员群: 
    第三群:2650485 
    第五群:29537639 
    第四群:28702746 
    第六群:10590618 
    第七群:10543585 
    第八群:12006492 
    第九群:19063074 
    第十群:2883885 
    第十一群:25460595 
    第十二群:9663807 深圳程序员QQ群联盟成立三年多,拥有三十个以上的QQ群,人数超二千多人,大量经验丰富的老手,成员从业于大公司(如微软、IBM,SUN,华为 )、来自国内著名高校和研究院成员,和有丰富实践经验的高级程序员、系统分析员(包括参加过上亿元的项目的架构师),有很热爱技术的成员 (包括自己写操作系统),还有少数女程序员。推荐:深程高级群I:17538442 深程高级群II:7120862 (深程高级群不欢迎新手,如果在深圳,月薪 6K以下的别加入) c++:15195967  mobile:31501597嵌入式:37489763  
    —————————————————————————————————————————— 
    如果你不是第一次看到此广告,说明我们最近T了一些人,因为我们要不断提升群的质量,保证名副其实. 
    ------------------------------------------------------------------------------------- 
    在通过电邮、新闻组或者聊天室提出技术问题前,检查你有没有做到: 
          1. 通读手册,试着自己找答案。 
          2. 在FAQ里找答案(一份维护得好的FAQ可以包罗万象:)。 
          3. 在网上搜索(个人推荐google~)。 
          4. 向你身边精于此道的朋友打听。 
    我想我们首先应该*自己解决问题,然后才是问! ***************************************************************************** 
      

  16.   

    呵呵 throw的想法是13楼的yonghengdexingxing兄最先提出的 我只推舟~~
      

  17.   

    回24楼1.首先我已经说了,不用return2.我不想在check中try(每个页面的check都不相同,一个页面就有一个,这样try也太多了),
    想在isNone中try(因为它只有一个)

    真心感谢大家的帮助!
      

  18.   

    感谢大家!!!
    根据22楼思维(22楼不如23楼,但23是吸取了22的思想),问题已经解决!tengfei3003  2楼,5楼,9楼,12楼,
    YH_Random  4楼,6楼
    yonghengdexingxing 8楼,13楼_,28楼,
    neo_yoho 15楼,24楼,30楼,
    hehe2006jian 20楼(不知其意)
    xf_pan 22楼(有错误,但是思路正确)
    chinmo 23楼(由22楼而来),25楼,26楼,27楼,
    sz000000001 29楼(广告)
    问题已经解决,等我处理手头事情立即结帖
      

  19.   

    你用retrun解决的?我并没有根据22楼的思想,作为这种检测方式我一向如此
      

  20.   

    回35楼不好意思,我看23楼和22楼的代码很相似(只不过22楼有点小错误),所以以为你从22楼吸取了思路。不过,22楼和23楼的思路是一样的。我没有用return,我在正在整理资料,一会把结果发上来。
      

  21.   

    问题答案:
    //其它函数
    String.prototype.trim = function ()
    {
    return this.replace(/(^\s*)|(\s*$)/g, "");
    }function $(objID) //根据ID返回对象
    {
    return document.getElementById(objID);
    }
    function checkForm()
    {
    this.flag = true;
    this.txtObj;
    this.tipStr;

    this.getTextObject = function (txtID,tips) //参数说明:txtID 文本框ID,tips 错误提示信息
    {
    this.txtObj = $(txtID);
    this.tipStr = tips;
    }

    this.errorTip = function () //错误提示
    {
    window.alert(this.tipStr);
    this.txtObj.focus(); event.returnValue = false;
    event.cancel = true;
    }


    this.isNone = function (txtID,tips) //空值检测
    {
    this.getTextObject(txtID,tips); if (this.txtObj.value.trim().length == 0 && this.flag) 
    {
    this.flag = false;
    this.errorTip();
    }
    }
    }<form id="soform" name="soform" method="post" action="Count.asp" onsubmit="cf();">
    <input type="text" id="username" name="username" />用户名<br />
    <input type="text" id="postcode" name="postcode" />邮编<br />
    <input type="text" id="handset" name="handset" />手机<br />
    <input type="text" id="idcard" name="idcard" />身份证<br />
    <input type="text" id="qq" name="qq" />QQ<br />
    <input type="text" id="email" name="email" />email<br /><input type="submit" value="提交" />
    <input type="reset" value="重置" />
    </form><script language="javascript" type="text/javascript">
    var c = new checkForm();function cf()
    {
    c.flag = true;
    c.isNone("username","用户名不能为空");
    c.isNone("postcode","邮编不能为空");
    c.isNone("handset","手机不能为空");
    }
    </script>
    目前还不能兼容Firefox,希望高手指教!
      

  22.   

    最终答案(兼容Firefox)//其它函数
    String.prototype.trim = function ()
    {
    return this.replace(/(^\s*)|(\s*$)/g, "");
    }function $(objID) //根据ID返回对象
    {
    return document.getElementById(objID);
    }function isIE() //检测浏览器类型
    {
    if (window.ActiveXObject)
    return true;
    else
    return false;
    }function checkForm()
    {
    this.eventObj;
    this.flag = true;
    this.txtObj;
    this.tipStr;

    this.getTextObject = function (txtID,tips) //参数说明:txtID 文本框ID,tips 错误提示信息
    {
    this.txtObj = $(txtID);
    this.tipStr = tips;
    }

    this.errorTip = function () //错误提示
    {
    window.alert(this.tipStr);
    this.txtObj.focus(); if (isIE())
    this.eventObj.returnValue = false;
    else
    this.eventObj.preventDefault();
    }


    this.isNone = function (txtID,tips) //空值检测
    {
    this.getTextObject(txtID,tips); if (this.txtObj.value.trim().length == 0 && this.flag) 
    {
    this.flag = false;
    this.errorTip();
    }
    }
    }<form id="soform" name="soform" method="post" action="Count.asp" onsubmit="cf(event);">
    <input type="text" id="username" name="username" />用户名<br />
    <input type="text" id="postcode" name="postcode" />邮编<br />
    <input type="text" id="handset" name="handset" />手机<br />
    <input type="text" id="idcard" name="idcard" />身份证<br />
    <input type="text" id="qq" name="qq" />QQ<br />
    <input type="text" id="email" name="email" />email<br /><input type="submit" value="提交" />
    <input type="reset" value="重置" />
    </form><script language="javascript" type="text/javascript">
    var c = new checkForm();function cf(eventObject)
    {
    c.eventObj = eventObject;
    c.flag = true; c.isNone("username","用户名不能为空");
    c.isNone("postcode","邮编不能为空");
    c.isNone("handset","手机不能为空");
    }
    </script>