调试一个文本对象的应用的例子,点击按钮,总是提示“找不到对象”,请大家帮我看看,多谢了。
代码如下:
<!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 language="javascript">
//判断字符s是否为空
function checkValid(s)
{
var len=s.length;
for(var i=0;i<len;i++)
{
if(s.charAt(i)!="")
{
return false;
}
}
return true;
}
//用户名称文本框失去焦点执行的函数
function nameBlur()
{
//首先用户名称不能为空
if(checkValid(document.baseInfo.userName.value))
{
window.alert("用户名称不能为空!");
}
else
{
//然后用户名称不能为默认值
if(document.baseInfo.userName.value==document.baseInfo.userName.defaultValue)
{
window.alert("用户名称不能为默认值");
}
}
}
//用户密码文本框失去焦点执行的函数
function pswBlur()
{
//首先用户密码不能为空
if(checkValid(document.baseInfo.psw.value))
{
window.alert("用户密码不能为空!");
}
else
{
//然后用户密码不能小于4位
if(document.baseInfo.psw.value.length<4)
{
window.alert("然后用户密码不能小于4位!");
}
}
}
//检查文本框中获得数据是否合法
function dataCheck()
{
//第一步:用户名称不能为空
if(checkValid(document.baseInfo.userName.value))
{
window.alert("用户名称不能为空!");
document.baseInfo.userName.focus();
}
else
{
//第二步:用户名称不能为默认值
{
if(document.baseInfo.userName.value==document.baseInfo.userName.defaultValue)
{
window.alert("用户名称不能为默认值!");
document.baseInfo.userName.focus();
}
else
{
//第三步:用户密码不能为空
if(checkValid(document.baseInfo.psw.value))
{
window.alert("用户密码不能为空!");
document.baseInfo.psw.focus();
}
else
{
//第四步:用户密码不能小于4位
if(document.baseInfo.psw.value.length<4)
{
window.alert("用户密码不能小于4位!");
document.baseInfo.psw.focus();
}
else
{
window.alert("你输入了正确的数据!")
}
}
}
}
}
//使用button对象重置表单
function resetForm()
{
document.baseInfo.userName.value="";
document.baseInfo.psw.value="";
document.baseInfo.content.value="";
}
</script>
</head>
<body onLoad="document.baseInfo.userName.focus()">
<h3>文本对象的使用</h3>
<form name="baseInfo">
<font size="2">用户名称:</font>
<input type="text" name="userName" value="用户名称不能为空" size=35
onFocus="this.select()" onBlur="nameBlur()">
<br><font size="2">用户密码:</font>
<input type="password" name="psw" maxlength=12 size="39" onFocus="this.select()" onBlur="pswBlur()"><br />
<font size="2">个性签名:</font>
<textarea name="content" rows=5 cols="34" onFocus="this.select()">
这家伙没什么个性!
</textarea><br /><br />
<input type="button" name="showPsw" value="查看密码" onClick="alert('用户输入密码是:'+document.baseInfo.psw.value)" />
<input type="button" name="btForm" value="数据检测" onClick="dataCheck()">
<input type="button" name="rsForm" value="数据清空" onClick="resetForm()">
</form>
</body>
</html>
代码如下:
<!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 language="javascript">
//判断字符s是否为空
function checkValid(s)
{
var len=s.length;
for(var i=0;i<len;i++)
{
if(s.charAt(i)!="")
{
return false;
}
}
return true;
}
//用户名称文本框失去焦点执行的函数
function nameBlur()
{
//首先用户名称不能为空
if(checkValid(document.baseInfo.userName.value))
{
window.alert("用户名称不能为空!");
}
else
{
//然后用户名称不能为默认值
if(document.baseInfo.userName.value==document.baseInfo.userName.defaultValue)
{
window.alert("用户名称不能为默认值");
}
}
}
//用户密码文本框失去焦点执行的函数
function pswBlur()
{
//首先用户密码不能为空
if(checkValid(document.baseInfo.psw.value))
{
window.alert("用户密码不能为空!");
}
else
{
//然后用户密码不能小于4位
if(document.baseInfo.psw.value.length<4)
{
window.alert("然后用户密码不能小于4位!");
}
}
}
//检查文本框中获得数据是否合法
function dataCheck()
{
//第一步:用户名称不能为空
if(checkValid(document.baseInfo.userName.value))
{
window.alert("用户名称不能为空!");
document.baseInfo.userName.focus();
}
else
{
//第二步:用户名称不能为默认值
{
if(document.baseInfo.userName.value==document.baseInfo.userName.defaultValue)
{
window.alert("用户名称不能为默认值!");
document.baseInfo.userName.focus();
}
else
{
//第三步:用户密码不能为空
if(checkValid(document.baseInfo.psw.value))
{
window.alert("用户密码不能为空!");
document.baseInfo.psw.focus();
}
else
{
//第四步:用户密码不能小于4位
if(document.baseInfo.psw.value.length<4)
{
window.alert("用户密码不能小于4位!");
document.baseInfo.psw.focus();
}
else
{
window.alert("你输入了正确的数据!")
}
}
}
}
}
//使用button对象重置表单
function resetForm()
{
document.baseInfo.userName.value="";
document.baseInfo.psw.value="";
document.baseInfo.content.value="";
}
</script>
</head>
<body onLoad="document.baseInfo.userName.focus()">
<h3>文本对象的使用</h3>
<form name="baseInfo">
<font size="2">用户名称:</font>
<input type="text" name="userName" value="用户名称不能为空" size=35
onFocus="this.select()" onBlur="nameBlur()">
<br><font size="2">用户密码:</font>
<input type="password" name="psw" maxlength=12 size="39" onFocus="this.select()" onBlur="pswBlur()"><br />
<font size="2">个性签名:</font>
<textarea name="content" rows=5 cols="34" onFocus="this.select()">
这家伙没什么个性!
</textarea><br /><br />
<input type="button" name="showPsw" value="查看密码" onClick="alert('用户输入密码是:'+document.baseInfo.psw.value)" />
<input type="button" name="btForm" value="数据检测" onClick="dataCheck()">
<input type="button" name="rsForm" value="数据清空" onClick="resetForm()">
</form>
</body>
</html>
你的{}嵌套得太多了,漏了个 }
嵌套加嵌套,不晕才乖呢,直接if也比这样强哦<!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 language="javascript">
//判断字符s是否为空
function checkValid(s)
{
var len=s.length;
for(var i=0;i <len;i++)
{
if(s.charAt(i)!="")
{
return false;
}
}
return true;
}
//用户名称文本框失去焦点执行的函数
function nameBlur()
{
//首先用户名称不能为空
if(checkValid(document.baseInfo.userName.value))
{
window.alert("用户名称不能为空!");
}else {
//然后用户名称不能为默认值
if(document.baseInfo.userName.value==document.baseInfo.userName.defaultValue)
{
window.alert("用户名称不能为默认值");
}
}
}
//用户密码文本框失去焦点执行的函数
function pswBlur()
{
//首先用户密码不能为空
if(checkValid(document.baseInfo.psw.value))
{
window.alert("用户密码不能为空!");
}else {
//然后用户密码不能小于4位
if(document.baseInfo.psw.value.length <4)
{
window.alert("然后用户密码不能小于4位!");
}
}
}
//检查文本框中获得数据是否合法
function dataCheck()
{
//第一步:用户名称不能为空
if(checkValid(document.baseInfo.userName.value))
{
window.alert("用户名称不能为空!");
document.baseInfo.userName.focus();
} else {
//第二步:用户名称不能为默认值
if(document.baseInfo.userName.value==document.baseInfo.userName.defaultValue)
{
window.alert("用户名称不能为默认值!");
document.baseInfo.userName.focus();
}
else
{
//第三步:用户密码不能为空
if(checkValid(document.baseInfo.psw.value))
{
window.alert("用户密码不能为空!");
document.baseInfo.psw.focus();
}
else
{
//第四步:用户密码不能小于4位
if(document.baseInfo.psw.value.length <4)
{
window.alert("用户密码不能小于4位!");
document.baseInfo.psw.focus();
}
else
{
window.alert("你输入了正确的数据!")
}
}
}
}
}
//使用button对象重置表单
function resetForm()
{
document.baseInfo.userName.value="";
document.baseInfo.psw.value="";
document.baseInfo.content.value="";
}
</script>
</head>
<body onLoad="document.baseInfo.userName.focus()">
<h3>文本对象的使用 </h3>
<form name="baseInfo">
<font size="2">用户名称: </font>
<input type="text" name="userName" value="用户名称不能为空" size=35
onFocus="this.select()" onBlur="nameBlur()">
<br> <font size="2">用户密码: </font>
<input type="password" name="psw" maxlength=12 size="39" onFocus="this.select()" onBlur="pswBlur()"> <br />
<font size="2">个性签名: </font>
<textarea name="content" rows=5 cols="34" onFocus="this.select()">
这家伙没什么个性!
</textarea> <br /> <br />
<input type="button" name="showPsw" value="查看密码" onClick="alert('用户输入密码是:'+document.baseInfo.psw.value)" />
<input type="button" name="btForm" value="数据检测" onClick="dataCheck()">
<input type="button" name="rsForm" value="数据清空" onClick="resetForm()">
</form>
</body>
</html>