专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Java Web开发

JavaScript不起作用,求解释?该如何解决

发布时间:2010-05-31 07:01:32 文章来源:www.iduyao.cn 采编人员:星星草
JavaScript不起作用,求解释?
HTML code

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">

    var userName = document.getElementById("userName");
    var password = document.getElementById("password");
    
    function check()
    {
        if(userName.value == "")
        {
            alert("用户名不能为空!");
            return false;
        }
        
        if(password.value == "")
        {
            alert("密码不能为空");
            return false;
        }
        
        if(userName.value.length<4||userName.length>10)
        {
            alert("用户名长度要在4到10之间");
            return false;
        }
        
        if(password.value.length<4||password.length>10)
        {
            alert("密码长度要在4到10之间");
            return false;
        }
        
        return true;
    }
    
</script>
</head>

<body>
<form action="#" method="post" onsubmit="return check()">

    用户名:
      <input type="text" name="userName" id="userName" />
      <br />
    密&nbsp;&nbsp;码:
    <input type="password" name="password" id="password" />
    <br />
    性&nbsp;&nbsp;别:
    <input type="radio" name="sex" value="1" />
    男
              <input type="radio" name="sex" value="0" />
              女<br />
    兴&nbsp;&nbsp;趣:
        足球
    <input type="checkbox" name="checkbox" value="0" />
        篮球
        <input type="checkbox" name="checkbox" value="1" />
        排球
        <input type="checkbox" name="checkbox" value="2" />
        羽毛球
        <input type="checkbox" name="checkbox" value="3" />
        <br />
    地&nbsp;&nbsp;址:
        <select name="select">
          <option value="0" selected="selected">北京</option>
          <option value="1">上海</option>
          <option value="3">陕西</option>
      </select><br />
      <input type="submit" value="点击确认" name="button1" />
      <input type="reset" value="重置" name="button2" />
       
</form>
</body>
</html>




还有我的Dreamweaver为什么在编写javaScript时没有提示?

------解决方案--------------------
var userName = document.getElementById("userName");
var password = document.getElementById("password");
放在function中
------解决方案--------------------
确实。作用域不对~~~你参数没有传到函数中。

按楼上的操作吧。
------解决方案--------------------

JScript code


    var userName = document.getElementById("userName");
    var password = document.getElementById("password");

------解决方案--------------------
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: