回 帖 发 新 帖 刷新版面

主题:为什么会提示“错误: 缺少对象”

刚刚学了点javascript,照着别人的东西自己改了一个测试用户名的程序,但是一运行就报错,提示“错误: 缺少对象”(粉色的那一行),自己怎么看都不知道问题所在,希望高手能帮忙看看错在哪里,还有就是再帮我看看这个程序还有没有其他错误,谢谢了


<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" language="javascript">
var profile_username_toolong = '对不起,您的用户名超过 15 个字符,请输入一个较短的用户名。';
var profile_username_tooshort = '对不起,您输入的用户名小于4个字符, 请输入一个较长的用户名。';
var profile_username_illegal = '用户名包含敏感字符或被系统屏蔽,注意用户名只能使用英文和数字,请重新填写。';

function checkusername() {
[color=FF00FF]var username = getElementById("username").value;[/color]username = trim(username)
if(username == lastusername) {
return;
} else {
lastusername = username;
}
var cu = getElementById("checkusername");
var unlen = username.replace(/[^\x00-\xff]/g, "**").length;
if (username == '' || /^[a-zA-Z0-9_]+$/.test('username')!="ture"){
warning(cu, profile_username_illegal);
}else{
return;
if(unlen < 4 || unlen > 15) {
warning(cu, unlen < 4 ? profile_username_tooshort : profile_username_toolong);
return;
}
}
}      
</script>
</head>

<body>
<form name="reg" method="post" action="freg.asp">
  <table cellspacing="0" cellpadding="4" width="598" align="center">
    <!--DWLayoutTable-->
       <tr>
      <td bgcolor="#D2DBE2" colspan="2" class="color1"><!--DWLayoutEmptyCell-->&nbsp;</td>
    </tr>
    <tr>
      <td width="70" class="text1" >用户名:</td>
      <td width="482" class="text1">
      <div style="width:100"><input type="text" name="username" size="25" maxlength="15" id="username" class="input" onBlur="checkusername()"></div>
      <div id="checkusername"></div>
      </td>
    </tr>
     </table>
</form>

</body>
</html>

回复列表 (共7个回复)

沙发

试试看 document.reg.username.value

板凳

var username = document.getElementById("username").value;这样试试

3 楼

上面的两种都试过了,但是没什么变化。

4 楼

我对代码进行了一点修改,加入红色的部分,同时把输入框的onblur事件连接到新加入的函数部分,这次测试完全正常,但是改回checkusername()函数后,问题依旧。

不知道是怎么回事

<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" language="javascript">
var profile_username_toolong = '对不起,您的用户名超过 15 个字符,请输入一个较短的用户名。';
var profile_username_tooshort = '对不起,您输入的用户名小于4个字符, 请输入一个较长的用户名。';
var profile_username_illegal = '用户名包含敏感字符或被系统屏蔽,注意用户名只能使用英文和数字,请重新填写。';
[color=FF0000]function chkacc(){
alert(document.getElementById("username").value);
}[/color]
function checkusername() {
[color=FF00FF]var username = getElementById("username").value;[/color]

username = trim(username)
if(username == lastusername) {
return;
} else {
lastusername = username;
}
var cu = getElementById("checkusername");
var unlen = username.replace(/[^\x00-\xff]/g, "**").length;
if (username == '' || /^[a-zA-Z0-9_]+$/.test('username')!="ture"){
warning(cu, profile_username_illegal);
}else{
return;
if(unlen < 4 || unlen > 15) {
warning(cu, unlen < 4 ? profile_username_tooshort : profile_username_toolong);
return;
}
}
}      
</script>
</head>

<body>
<form name="reg" method="post" action="freg.asp">
  <table cellspacing="0" cellpadding="4" width="598" align="center">
    <!--DWLayoutTable-->
       <tr>
      <td bgcolor="#D2DBE2" colspan="2" class="color1"><!--DWLayoutEmptyCell-->&nbsp;</td>
    </tr>
    <tr>
      <td width="70" class="text1" >用户名:</td>
      <td width="482" class="text1">
      <div style="width:100"><input type="text" name="username" size="25" maxlength="15" id="username" class="input" onBlur="[color=FF0000]chkacc()[/color]"></div>
      <div id="checkusername"></div>
      </td>
    </tr>
     </table>
</form>

</body>
</html>

5 楼

大家帮忙看看吧,顶一下

6 楼

lastusername这是什么?
function checkusername() {
var username = getElementById("username").value;//这里改成document.getElementById("username").value;
……
}

7 楼

终于解决了,有几个地方:
javascript里面没有trim函数,用之前没有构建这个函数,
还有就是warning函数也没有构建,
还有就是$的用法没有弄清楚,盲目的把所有的$都替换成getElementById
现在仍然有些疑问
$()到底是相当于什么?
getElementById()、
document.getElementById()、
document.getElementById().value???
上面那三个东西到底有什么区别?
比如有一个input
<input type="text" name="username" size="25" maxlength="15" id="username" class="input" onBlur="chkacc()" value="请输入用户名">
分别用上面的三个东西取值,结果是什么?



lastusername这个变量应该是上一次检测过的用户名,如果上次检测过的用户名和本次的一致,就不要执行下面的检测程序了。return应该是这个意思吧。



后加的两段函数:

function trim(str) {
return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');
}
function warning(obj, msg) {
if((ton = obj.id.substr(5, obj.id.length)) != 'password2') {
document.getElementById(ton).select();
}
obj.style.display = '';
obj.innerHTML = '<img src="images/blue/check_error.gif" width="13" height="13"> &nbsp; ' + msg;
obj.className = "warning";
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册