[英]Jquery bind event on element fails
We are using Jquery 2.1.4 and have written out own javascript class that should take care of the event. 我们使用的是Jquery 2.1.4,并编写了自己的javascript类来处理该事件。 It is a very simple "application".
这是一个非常简单的“应用程序”。 All it does is taking care of submitting a form.
它所做的只是照顾提交表格。 Before that, it processes the data.
在此之前,它会处理数据。
Our initial method that we call on rendering the page: 我们在呈现页面时调用的初始方法:
OWebForm.prototype.init = function(){
console.log("init Method called");
...
$("#submit_message").on("click", this._submit);
console.log($("#submit_message"));
...
}
OWebForm.prototype._submit = function(e){
e.preventDefault();
console.log("_submit Method called");
...
}
Once the button " #submit_message
" is clicked, it is supposed to call the _submit
method of the OWebForm class. 一旦单击按钮“
#submit_message
”,就应该调用OWebForm类的_submit
方法。 When looking at the element within the console I can see that it is not bound to anything, when the page is loaded. 在控制台中查看元素时,可以看到页面加载时未绑定任何元素。 Hence the code is not executed once the button is clicked.
因此,一旦单击按钮,就不会执行代码。
in the HTML I have the following code: 在HTML中,我有以下代码:
<script type="text/Javascript">
var _oWebForm = new OWebForm("0bcfwqx23xv02dfaqfujdqyafziic4b07uxkkg1y6lkof7x0px0vjm2tpaik2l2rmlrhnjya0bvctnpq26dqcom1ij5zpibodke3rs1z4f2syllthtj0kpl3p4vrw0vw");
_oWebForm.init();
</script>
From the documentation I understood, that the function has to exist before it is bound to an element event. 根据我了解的文档,该函数必须先存在,然后再绑定到元素事件。 Is this not the case when working with objects ?
使用对象时不是这种情况吗? How would I fix this ?
我该如何解决?
This works for me: 这对我有用:
var OWebForm = function(a){ }; OWebForm.prototype.init = function() { alert("init Method called"); $("#submit_message").on("click", this._submit); } OWebForm.prototype._submit = function(e){ e.preventDefault(); alert("_submit Method called"); } $(function() { var _oWebForm = new OWebForm("0bcfwqx23xv02dfaqfujdqyafziic4b07uxkkg1y6lkof7x0px0vjm2tpaik2l2rmlrhnjya0bvctnpq26dqcom1ij5zpibodke3rs1z4f2syllthtj0kpl3p4vrw0vw"); _oWebForm.init(); });
<script src="http://code.jquery.com/jquery-1.11.3.js"></script> <form id="myFrm"> <input type="text"> <input id="submit_message" type="submit" value="Click me To test"> </form>
You need to substitute: 您需要替换:
$("#submit_message").on("click", this._submit);
with: 与:
$(document).on("click", "#submit_message", this._submit);
If the submit_message is not already loaded! 如果尚未加载submit_message!
Your script is executed before the DOM is loaded so the element doesn't exist yet, and the jQuery selector doesn't match anything, so no elements get a click handler bound to them. 您的脚本是在DOM加载之前执行的,因此该元素尚不存在,并且jQuery选择器不匹配任何内容,因此没有元素绑定到其单击处理程序。 You need to call the
init()
method with in $(document).ready()
. 您需要在
$(document).ready()
调用init()
方法。
A page can't be manipulated safely until the document is "ready."
在文档“就绪”之前,无法安全地操纵页面。 jQuery detects this state of readiness for you.
jQuery为您检测到这种就绪状态。 Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
$(document).ready()内包含的代码仅在页面Document Object Model(DOM)准备好执行JavaScript代码后才能运行。
$(document).ready(function() {
var _oWebForm = new OWebForm("0bcfwqx23xv02dfaqfujdqyafziic4b07uxkkg1y6lkof7x0px0vjm2tpaik2l2rmlrhnjya0bvctnpq26dqcom1ij5zpibodke3rs1z4f2syllthtj0kpl3p4vrw0vw");
_oWebForm.init();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.