0

我正在尝试在提交表单字段之前填充它,但是该功能似乎被忽略了,并且不确定我是否做错了。为什么即使我返回 false 也允许提交表单?

代码

<!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="&copy; 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>
4

1 回答 1

4

您必须将返回值传递给事件处理程序本身:

<input ... onClick="return formDetails();" ... />
于 2012-07-22T23:23:41.430 回答