<%@ page language="java" pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=GBK">
  <title>易宝搜索</title>
 
 <style type="text/css">
label {font-size:14px;color:#1E90FF; cursor:pointer;font-weight: 580;}
</style><script type="text/javascript">
function checked() {
if(form.key.value == "") {
alert("false");
document.form1.key.focus(); 
return false;
}
return true;
}
</script>
</head> <body>
 <jsp:include page="/header.html" />
 <div align="center">
<form action="query.do" method="get" id="queryForm" onSubmit="return checked(this);">
  <input type="text" name="key" style="width:250px; height:18px;border:1px solid #D0D9F7; background-color:#F7F7F7;padding:5px; font-weight:bold; color:#666666; font-size:14px;"/>
  <input type="submit" value="易宝搜索" style="width:80px; height:28px; border:0; background:#00BFFF; color:#FFFFFF; font-size:14px; cursor:pointer;"/>
<div align="center">
  <label>
  <input type="radio" name="resourcetype" value="all" checked>
  全 部
  <input type="radio" name="resourcetype" value="doc">
  文 档
              <input type="radio" name="resourcetype" value="pic">
                图 片
              <input type="radio" name="resourcetype" value="audio">
                音 频
      <input type="radio" name="resourcetype" value="video">
       视 频
      </label>
 </div>
</form>
 </div>
 </body>
 
</html>现在如果key文本框中的内容为空时就不提交请求,感觉这个JavaScript函数没错啊,但就是不行。。(执行不到js里的checked函数)。那位帮忙看下吧!!谢谢

解决方案 »

  1.   

    form name漏了
    <form name="form1"....
      

  2.   

    给key加上id 
    <input type="text" id="key" name="key" style="width:250px; height:18px;border:1px solid #D0D9F7; background-color:#F7F7F7;padding:5px; font-weight:bold; color:#666666; font-size:14px;"/>
                  
      

  3.   

    还有为什么你form里是checked(this)带参数的,定义的是checked()无参的?改成一致的就好了吧
      

  4.   

    噢,,谢谢楼上的,
    那如果输入几个空格后,让表单提交不了呢?我用trim(form1.key.value) == "" 不行啊,谁帮帮忙??
      

  5.   

    <script type="text/javascript">
        function checked(formname) {
            if(form.key.value == "") {
                alert("false");
                document.form1.key.focus(); 
                return false;
            }
            return true;
        }
    </script>js方法中没有参数.
      

  6.   

    <script type="text/javascript">
        function checked(formname) {
            if(formname.key.value == "") {
                alert("false");
                document.form1.key.focus(); 
                return false;
            }
            return true;
        }
    </script>
    改过来试一试.
      

  7.   

    form1.key.value.replace(/\s/g,"")  得到去空格后的值
      

  8.   

    给from加上name属性,因为你在javascript函数里面引用到了,<form name="form1"....
    然后修改函数如下:就OK! 
        function checked() {
            if(form1.key.value == "") {
                alert("false");
                document.form1.key.focus(); 
                return false;
            }
            return true;
        }
      

  9.   

    为什么一定要用form获取值呢  直接document.getElementById("key").value  只要key在页面里就能得到  key要是ID
      

  10.   

    form1.key.value.replace(/\s/g,"")
    太棒了,就不用自己写trim函数了,,不过小弟初学,能不能解释一下这句话啊,,正则可以这样用吗?
      

  11.   

    (/\s/g)//创建正则表达式样式为
    \cx 匹配由x指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。 x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 'c' 字符。 
    \f 匹配一个换页符。等价于 \x0c 和 \cL。 
    \n 匹配一个换行符。等价于 \x0a 和 \cJ。 
    \r 匹配一个回车符。等价于 \x0d 和 \cM。 
    \s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。 
    \S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 
    \t 匹配一个制表符。等价于 \x09 和 \cI。 
    \v 匹配一个垂直制表符。等价于 \x0b 和 \cK。