js判断所有表单项不为空则提交表单

 

案例一:

<html>
  <head>
    <title>提交表单页</title>
    <script type="text/javascript">
      function myCheck()
      {
         for(var i=0;i<document.form1.elements.length-1;i++)
         {
          if(document.form1.elements[i].value=="")
          {
           alert("当前表单不能有空项");
           document.form1.elements[i].focus();
           return false;
          }
         }
         return true;
        
      }
    </script>
  </head>
  <body>
     <form name="form1" method="post" action="page2.html" onSubmit="return myCheck()">
       用户名:<input type="text" name="username"><br>
       性别:<input type="text" name="sex"><br>
       出生时间:<input type="text" name="birthday"><br>
       <input type="submit" value="提交">
     </form>
  </body>
</html>

案例二:

<script language="Javascript">
<!--
function submit1(frm){
  if (document.frm.word.value=="") {
    alert("请输入搜索新闻内容!");document.frm.word.focus();return false;
}
return true;
} 

//-->
</script>
<form method="POST" action="" name="frm" onsubmit="return submit1(this);">
<select name="select0"> 
<option value="0">请选择一个类别</option>
<option value="1">新闻</option>
<option value="2">产品</option>
<option value="3">留言</option>
</select>
<input type="text" name="word" size="20" class="input" style="height:19px; ">
<input type="radio" name="sea_cat" value="标题">标题  
<input type="radio" name="sea_cat" value="内容">内容
<input type="submit" value="搜" name="B1" class="input1" >

 

posted @ 2016-09-02 14:26  智昕  阅读(724)  评论(0编辑  收藏  举报