I have tried to search it but no favorable results found.
我试过搜索,但是没有找到好的结果。
I don't use JQuery and I want to make it with pure JavaScript. I want to make the same as <form onsubmit="somefunction();">
but directly from JavaScript.
我不使用JQuery,我想用纯JavaScript实现。我想让它与
Something like:
喜欢的东西:
if(document.getElementById("myid").ISSUBMIT) {
somefunction();
}
in Jquery I can make it with on.submit
, but how to do in JavaScript? Thanks a lot!
在Jquery中,我可以使用on。提交,但是如何使用JavaScript?谢谢!
2 个解决方案
#1
7
You get a reference to the form element, and then either:
你会得到表单元素的引用,然后:
-
Assign a function to
onsubmit
为onsubmit分配一个函数
theFormElement.onsubmit = function() { /* ... */ };
or
或
-
Use
addEventListener
/attachEvent
to hook thesubmit
event. If you don't need IE8 support, you can just useaddEventListener
:使用addEventListener / attachEvent挂起提交事件。如果你不需要IE8支持,你可以使用addEventListener:
theFormElement.addEventListener("submit", function(e) { // your code here }, false);
If you also need IE8 support, you probably want to use a function that handles the fact that IE8 uses
attachEvent
instead ofaddEventListener
, such as the one in this answer.如果您还需要IE8支持,您可能需要使用一个函数来处理IE8使用attachEvent而不是addEventListener,例如这个答案中的一个。
To get a reference to the form, you can use an id
on the form and use document.getElementById
, or (on any modern browser, and even IE8) you can use document.querySelector
and any CSS selector that will find the form.
要获取对表单的引用,可以在表单上使用id并使用文档。getElementById,或者(在任何现代浏览器上,甚至是IE8上)可以使用document。querySelector和任何会找到表单的CSS选择器。
#2
5
You can use JavaScript's "onsubmit" event.
您可以使用JavaScript的“onsubmit”事件。
Html:
Html:
<form id="my-id">
// elements
</form>
Javascript:
Javascript:
var form = document.getElementById( 'my-id' );
form.onsubmit = function(){
// your code
};
#1
7
You get a reference to the form element, and then either:
你会得到表单元素的引用,然后:
-
Assign a function to
onsubmit
为onsubmit分配一个函数
theFormElement.onsubmit = function() { /* ... */ };
or
或
-
Use
addEventListener
/attachEvent
to hook thesubmit
event. If you don't need IE8 support, you can just useaddEventListener
:使用addEventListener / attachEvent挂起提交事件。如果你不需要IE8支持,你可以使用addEventListener:
theFormElement.addEventListener("submit", function(e) { // your code here }, false);
If you also need IE8 support, you probably want to use a function that handles the fact that IE8 uses
attachEvent
instead ofaddEventListener
, such as the one in this answer.如果您还需要IE8支持,您可能需要使用一个函数来处理IE8使用attachEvent而不是addEventListener,例如这个答案中的一个。
To get a reference to the form, you can use an id
on the form and use document.getElementById
, or (on any modern browser, and even IE8) you can use document.querySelector
and any CSS selector that will find the form.
要获取对表单的引用,可以在表单上使用id并使用文档。getElementById,或者(在任何现代浏览器上,甚至是IE8上)可以使用document。querySelector和任何会找到表单的CSS选择器。
#2
5
You can use JavaScript's "onsubmit" event.
您可以使用JavaScript的“onsubmit”事件。
Html:
Html:
<form id="my-id">
// elements
</form>
Javascript:
Javascript:
var form = document.getElementById( 'my-id' );
form.onsubmit = function(){
// your code
};