【发布时间】:2009-04-02 15:49:05
【问题描述】:
我想知道如何从表单中获取onsubmit 事件来进行表单验证,因为我无法直接访问它。 (我正在为 cmets 编写一个 Wordpress 插件,所以不能直接访问表单标签或提交按钮。)
尝试为我的插件执行此操作感到非常沮丧,因此我在下面编写了一个 Hello World 版本。我希望它在我加载页面时显示“Hello World”警报,并在我单击提交按钮时显示“表单提交”警报。相反,它会在页面加载时显示两个弹出窗口。
这是我的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Test</h2>
<form action="#" method="post" id="commentform">
<p><input type="text" name="author" id="author" size="22" tabindex="1" />
<label for="author"><small>Name (required)</small></label></p>
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
</form>
<script type="text/JavaScript">
<!--
alert("Hello world");
var formCheck = document.getElementById("commentform");
formCheck.onSubmit = doMapping();
function doMapping() {
alert("form submitted");
return false;
}
-->
</script>
</body>
</html>
【问题讨论】:
标签: javascript html forms