我试图在提交表单字段之前填充表单字段但是该函数似乎被忽略并且不确定我是否正在错误地执行此操作。为什么即使我返回假,也允许提交表单?
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="pragma" content="no-cache" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="copyright" content="© 2012" />
<meta name="robot" content="noindex, nofollow" />
<title> sample form</title>
<base href="" />
<link rel="stylesheet" type="text/css" media="all" href="" />
<style type="text/css">
</style>
<script type="text/javascript">
function formDetails() {
var name = document.forms["sampleform"]["name"].value = "test"
return false;
}
</script>
</head>
<body>
<form id="sampleform" name="sampleform" action="" method="get">
<p><label for="name">Name</label>
<input type="text" name="name" id="name" maxlength="50" /></p>
<p><input type="submit" name="submit" value="submit" onClick="formDetails();" /></p>
</form>
</body>
</html>
答案 0 :(得分:4)
您必须将返回值传递给事件处理程序本身:
<input ... onClick="return formDetails();" ... />