如何获取表单的onSubmit事件? [英] How to grab the onSubmit event for a form?
问题描述
我想知道如何从窗体抓取 onsubmit
事件来进行表单验证,因为我无法直接访问它。 (我正在写注释的Wordpress插件,所以不能直接访问表单标签或提交按钮。)
我非常沮丧地尝试这样做对于我在下面编写Hello World版本的插件。我希望它在加载页面时显示'Hello World'警报,当点击提交按钮时显示提交表单警报。相反,它会在页面加载时显示弹出窗口。
以下是我的代码:
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.0 Transitional // EN>
< html>
< head>
< title> Hello World< / title>
< / head>
< body>
< h2>测试< / h2>
< form action =#method =postid =commentform>
< p>< input type =textname =authorid =authorsize =22tabindex =1/>
< label for =author>< small>名称(必填)< /小>< / label>< / p>
< p>< input name =submittype =submitid =submittabindex =5value =Submit Comment/>
< / form>
< script type =text / JavaScript>
<! -
alert(Hello world);
var formCheck = document.getElementById(commentform);
formCheck.onSubmit = doMapping();
函数doMapping(){
alert(form submitted);
返回false;
}
- >
< / script>
< / body>
< / html>
更改:
formCheck.onSubmit = doMapping()
<
formCheck.onSubmit = doMapping
当您将括号添加到函数的末尾时,您将执行该函数。当你分配一个函数(或者将它作为参数传递给另一个函数)时,你需要省略括号,因为这是在JavaScript中检索函数指针的方法。
编辑:您还需要将 doMapping
函数的声明移动到函数到这样的onsubmit事件(好赶tvanfosson!):
$ b $ pre $函数doMapping(){
alert(表格提交);
返回false;
}
formCheck.onSubmit = doMapping();
但是,如果 doMapping
函数未被使用在其他地方,您可以声明 doMapping
函数为这样的匿名函数:
formCheck.onSubmit = function(){
alert(form submitted);
返回false;
}
这对我来说似乎更清洁一些。
I want to know how to grab the onsubmit
event from a form to do some form validation, because I don't have access to it directly. (I am writing a Wordpress plugin for comments, so don't have direct access to the form tag or the submit button.)
I got so frustrated trying to do this for my plugin that I have written a Hello World version below. I want it to show the 'Hello World' alert when I load the page, and the "form submitted" alert when I click on the submit button. Instead, it shows both pop ups when the page loads.
Here is my code:
<!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>
Change this:
formCheck.onSubmit = doMapping()
to this:
formCheck.onSubmit = doMapping
When you add parenthesis to the end of a function you execute that function. When you assign a function (or pass it as a parameter to another function) you need to omit the parenthesis as that is the way to retrieve a function pointer in JavaScript.
Edit: You will also need to move the declaration of the doMapping
function above the assignment of that function to the onsubmit event like this (good catch tvanfosson!):
function doMapping() {
alert("form submitted");
return false;
}
formCheck.onSubmit = doMapping();
However if the doMapping
function is not used elsewhere you can declare the doMapping
function as an anonymous function like this:
formCheck.onSubmit = function() {
alert("form submitted");
return false;
}
which seems a bit cleaner to me.
这篇关于如何获取表单的onSubmit事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!