<html>
<head>
<script type="text/javascript">function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {alert(alerttxt);
 document.getElementById("test").value = "Changed!";
 return false
}
  else {return true}
  }
}function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(email,"Email must be filled out!")==false)
    {email.focus();return false}
  }
}
</script>
</head><body>
<form action="submitpage.htm" onSubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit"> 
</form>
<p name = "test" >prepare to be changed</p>
</body>
</html>当代吗运行到alert(alerttxt)时,会弹出警示框,按说表单不会被提交,可是当我点击警示矿上的确认按钮后,表单被提交了!如果我将document.getElementById("test").value = "Changed!"注销,表单是不会被提交的,为什么加上这句话就不对了呢?
还有一个小问题,什么叫文档加载后在使用document.write()就会覆盖原来的文档啊?请大家帮忙举个例子吧。
本人初学。越详细越好。谢谢啦~JavaScriptonsubmit

解决方案 »

  1.   

    document.getElementById("test").value = "Changed!";因为你的html页面里没有id为test的节点,所以document.getElementById("test")返回的是null。但是null是没有value属性的,所以document.getElementById("test").value会错误的,是异常的。但是你没有异常处理,所以直接函数就结束了,函数没有返回false,默认是返回true的,所以没能阻止表单提交。关于document.write(),如果页面已经加载完成,也就是说,document.write()是在window.onload后执行的,那么,使用document.write()输出的内容,会覆盖掉原来document中的内容。就好像你在黑板上写好了字,然后再使用document.write()去写,document.write()会先把黑板擦干净,再去写它要写的内容。
      

  2.   

    嗯。第一个问题我明白了(还写错了一个地方,应该是“document.getElementById("test").innerText = "Changed!";”)。第二个问题,我在“<p name = "test" >prepare to be changed</p>”后加上“<script>document.write()</script>”,网页应该已经加载完了吧,为什么还是能显示全部内容呢?
      

  3.   

    第一个问题
    <p name = "test" >prepare to be changed</p>
    name改成id
    第二个问题
    <!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>
    <title> new document </title>
    </head>
    <body>
    页面原来的内容<br />
    页面原来的内容<br />
    页面原来的内容<br />
    页面原来的内容<br />
    页面原来的内容<br />
    页面原来的内容<br />
    页面原来的内容<br />
    页面原来的内容<br />
    <script type="text/javascript">
    function aaa()
    {
    document.write("<p>原来的内容全没了</p>");
    alert(document.body.innerHTML);
    }
    setTimeout(aaa,10000);//10秒后网页应该已经加载完
    </script>
    </body>
    </html>
      

  4.   

    document.write一般是在加载过程中使用来实现动态,这时候它处在流中。但是一旦页面加载完成,那么再执行的,他就要新开始一个流,而这个流只有你要write的内容,浏览器重新paint页面,所以原来页面被清光了。