一个关于onsubmit的问题

li_589 2008-07-27 07:17:01
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en">
<head>
<title>onsubmit event</title>
<script type = "text/javascript">
function writeMessage() {
document.getElementById('text2').value = document.getElementById('text1').value;
}
function message1() {
return false;
}
function message2() {
return true;
}
function message3() {
document.write("hello");
return true;
}
</script>
</head>
<body>
<p>The onkeydown event occurs when the a keybord key is on its way UP</p>
<form onsubmit = "return message3()" action = "http://www.hao123.com">
Enter something:
<input type = "text" id = "text1" onkeyup = "writeMessage()" /> <!-- 当是onkeydown时 text1中最后按下的那个字符还没有给text2所发两个文本框相差一个字符 keyup才表示一个输入完成 -->
<input type = "text" id = "text2" />
<input type = "submit" value = "提交">
</form>
</body>
</html>
message3() 的返回值是true为什么不提交到http://www.hao123.com这个页面呢?反而跳到了另一个页面,最好能详细的讲解一下
...全文
136 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicholsky 2008-07-27
  • 打赏
  • 举报
回复
6楼讲得很清楚了,楼主这个不是onsubmit的问题,应该是文档未载入和已载入后document.write()的区别问题。
阿云ivan 2008-07-27
  • 打赏
  • 举报
回复
不要用document.write("hello");
它会把当前页的内容抹掉

因此即使return true,也不会action到你指定的那个页面
lawrendc 2008-07-27
  • 打赏
  • 举报
回复
因为你是在页面已经加载完毕之后对页面进行的document.write操作

它将会重构页面 也就是说你的form已经不存在了 哪来的提交

你试试这个代码

function message3() {
alert(document.forms.length);
//alert("hello");
document.write("hello");
alert(document.forms.length); //得到的是0 也就是form已经不存在了
return true;
}
familyX 2008-07-27
  • 打赏
  • 举报
回复
去掉document.write
li_589 2008-07-27
  • 打赏
  • 举报
回复
我知道去掉就可以,我想知道加上那句为什么就不可以了?
lawrendc 2008-07-27
  • 打赏
  • 举报
回复

function message3() {
document.write("hello");
return true;
}



你改成


function message3() {
alert("hello");
//document.write("hello");
return true;
}



就可以
lawrendc 2008-07-27
  • 打赏
  • 举报
回复
document.write重写了页面之后

还怎么提交??
tantaiyizu 2008-07-27
  • 打赏
  • 举报
回复
为什么要 document.write 去掉这句看看

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧