我正在做的事可能是愚蠢的.我想在提交时使用表单的值填充隐藏的DIV.
DIV确实打开了正确的数据,但在页面加载完成后重置.我究竟做错了什么?
这是我的测试:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii" />
<title>Test</title>
<script type="text/javascript">
function test(){
var usr = document.getElementById('user').value;
var pwd = document.getElementById('passwd').value;
document.getElementById('out').innerHTML = usr + " " + pwd;
document.getElementById('out').style.display = "block";
return true;
}
</script>
</head>
<body>
<form action="" onsubmit="return test()">
<input type="text" id="user" name="user" />
<input id="passwd" type="text" name="passwd" />
<p><input type="submit" value="Go" /></p>
</form>
<div id="out" style="display:none;">
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
简短回答:
改变这个
return true;
Run Code Online (Sandbox Code Playgroud)
对此
return false;
Run Code Online (Sandbox Code Playgroud)
答案很长:
表单旨在在提交新页面时加载.但是,通过脚本编写,我们可以通过停止提交事件来防止此行为.这可以通过多种方式来实现,但是对于你的榜样,简单地返回从处理程序"假",将取消该事件,但只有当的onsubmit属性也有一个return语句(你已经有了).