日期:2014-05-18  浏览次数:20689 次

遇到一个奇怪的问题,关于jsp和javascript的问题
为什么我在form中加了<input type="hidden" name="action" value="post">提交隐藏变量时,就不能引用javascript来进行客户端的是否为空的验证,而不加时就可以验证呢??

<%
request.setCharacterEncoding("GBK");
String action = request.getParameter("action");//接收下面的form提交的action
response.sendRedirect("article.jsp");
%>

//下面是javascript代码
<script language="JavaScript1.2" type="text/javascript">
<!--
function isEmpty()
{
var title = document.postForm.title.value;
var cont = document.postForm.cont.value;
if(title=="")
{
alert("主题不能为空!!");
return false;
}
if(title!="" && cont!="")
{
alert("提交成功");
return true;
}
}

//下面是form中的代码
<form name="postForm" action="post.jsp" method="post">  
//post.jsp是本身,就是form表单提交给本身 
<input type="hidden" name="action" value="post">
<table align="center" border="1" width="100%">
  <tr>
  <td class="jive-subject" align="left">
  <img src="images/up-10x10.gif">主题内容
  </td>
  </tr>
  <tr>
<td>
  <textarea rows="15" cols="150" name="cont"></textarea>
</td>
  </tr>
  <tr>
  <td align="center">
  <input type="submit" value="提交" onClick="isEmpty()"/>//调用isEmpty()
<input type="reset" value="重置">
  </td>
  </tr>
</table>
</form>

------解决方案--------------------
同意一楼 不应该写在submit 的onclick事件里
应该写在form的onsubmit事件里
------解决方案--------------------
你丢了脚本的结束标记了 真是粗心啊!

HTML code
<%   
request.setCharacterEncoding("GBK");   
String   action   =   request.getParameter("action");//接收下面的form提交的action   
response.sendRedirect("article.jsp");   
%>   

//下面是javascript代码   
<script       language="JavaScript1.2"       type="text/javascript">   
<!--   
function       isEmpty()   
{   
var       title       =       document.postForm.title.value;   
var       cont       =       document.postForm.cont.value;   
if(title=="")   
{   
alert("主题不能为空!!");   
return       false;   
}   
if(title!=""       &&       cont!="")   
{   
alert("提交成功");   
return       true;   
}   
}   
//-->
</script>

//下面是form中的代码   
<form           name="postForm"       action="post.jsp"       method="post"   onsubmit="return   isEmpty()">           
//post.jsp是本身,就是form表单提交给本身       
<input       type="hidden"       name="action"       value="post">   
<table       align="center"       border="1"       width="100%">   
                    <tr>   
                            <td> 
                                    <input   type="text"   name="title"/>       
                                                      </td>   
                    </tr>   
                            <tr>   
<td>   
                                        <textarea       rows="15"       cols="150"       name="cont">   </textarea&g