admin管理员组文章数量:1287200
I am trying to understand why onsubmit at Javascript is using an anonymous function to get it value like that:
onsubmit = function() {return validForm();}
I know that onsubmit is taking a true or false values, but i can't understand how does the anonymous function is necessary to get the value of true or false for the onsubmit event, like this:
onsubmit = validForm();
I will be very thankful if some one can please help me understand this, Thank you all and have a nice day.
I am trying to understand why onsubmit at Javascript is using an anonymous function to get it value like that:
onsubmit = function() {return validForm();}
I know that onsubmit is taking a true or false values, but i can't understand how does the anonymous function is necessary to get the value of true or false for the onsubmit event, like this:
onsubmit = validForm();
I will be very thankful if some one can please help me understand this, Thank you all and have a nice day.
Share Improve this question asked Jul 23, 2012 at 13:37 Aviel FedidaAviel Fedida 4,1029 gold badges57 silver badges89 bronze badges 2-
You can simple
onsubmit = validForm
– Andreas Louv Commented Jul 23, 2012 at 13:38 - Thank you guys i got it now, have a nice day. – Aviel Fedida Commented Jul 23, 2012 at 13:49
2 Answers
Reset to default 7A valid assignment would be:
onsubmit = validForm;
You're assigning the function called validForm as the onsubmit handler. But, when you do this:
onsubmit = validForm();
You're assigning the result of calling the function validForm to the onsubmit handler. Unless validForm is returning a function, this won't work.
The reason the anonymous function assignment above works is, it is assigning a function to the onsubmit handler -- a function that in turn calls the validForm function when it is called.
You are calling validForm
and setting onsubmit
to its return value. In example:
function foo () {
return "hi";
}
onsubmit = foo() // eq to onsubmit = "hi";
onsubmit = foo // onsubmit now has the same function as foo
本文标签: javascriptonsubmitStack Overflow
版权声明:本文标题:JavaScript, onsubmit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741219094a2360619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论