阚清子眼睛分析:Javascript问题请教

来源:百度文库 编辑:高考问答 时间:2024/05/01 20:14:12
<script language="javascript">
function String.prototype.Trim(){return this.replace(/^\s+|\s+$/g,"");}
String.prototype.Ltrim = function(){return this.replace(/^\s+/g, "");}
String.prototype.Rtrim = function(){return this.replace(/\s+$/g, "");}

function checkdata() {
if (document.form1.answer.value.Trim()=="") {
alert("请输入答案后再提交!");
document.form1.answer.focus();
return false;
}
return true;
}
</script>

提交处这样写:<input type='submit' value='提交' class=button style='cursor:hand;' onclick='return checkdata();'>

提交后总提示:
document.form1.answer.value 为空或不是对象,为什么呀,错在哪了?高人提点一下。

请确认:<input type='submit' value='提交' class=button style='cursor:hand;' onclick='return checkdata();'>
的表单开始的名称
<form name="form1" method="post"action="admin_check.asp?action=login" target="_blank">

name="form1"

if (document.form1.answer.value.Trim()=="")
你这里语法就有错误,没有.Trim方法的,要写也要写成这样:
if (Trim(document.form1.answer.value)=="")
这样你先在这里改正一下

<html>

<head>

<head>
<body>
<script language="javascript">
function String.prototype.Trim(){return this.replace

(/^\s+|\s+$/g,"");}
String.prototype.Ltrim = function(){return this.replace

(/^\s+/g, "");}
String.prototype.Rtrim = function(){return this.replace(/

\s+$/g, "");}

function checkdata()
{
var res = false;
if (document.all.answer.value.Trim()=="")
{
alert("请输入答案后再提交!");
document.form1.answer.focus();

res = false;
}
else
{
res = true;
}

alert(res);
}
</script>

<input type="text" value="text" name="answer" id="answer">
提交处这样写:<input type='button' value='提交' class=button

style='cursor:hand;' onclick=checkdata()>

</body>
</html>

你改正一下。。。我改了一下你的源码。。你把

document.form1.answer.value

改成

document.all.answer.value 就可以了。。。

原因可能是你把这段脚本放到<form>的上面了加载js脚本的时候还不能认到form1