简体   繁体   English

由于多个JavaScript包含/要求,jQuery事件多次触发

[英]jQuery event triggering multiple times because of multiple javascript inclusions/requires

I have a problem here and it's making me mad. 我在这里遇到问题,这使我发疯。 I maintain a legacy php system. 我维护一个遗留的php系统。 It's badly organized, no frameworks are used and a lot of other problems, for example, at least 5 different query versions are used in different parts of the system. 它的组织很糟糕,没有使用框架,还有很多其他问题,例如,在系统的不同部分中至少使用了5个不同的查询版本。

So my problem right now is this, I have a search form, when a button is clicked it shows a list of items, lets call this a list of "A" objects. 所以我现在的问题是,我有一个搜索表单,当单击一个按钮时,它显示一个项目列表,我们称其为“ A”对象列表。 In each A item, there is an expand /toggle button to show the B items that belong to A (this is done using ajax, by setting a specific div's html to the ajax response). 在每个A项目中,都有一个展开/ toggle按钮来显示属于A的B项目(这是通过使用ajax,通过将特定div的html设置为ajax响应来完成的)。 Then each B also has an expand/toggle button to show the C items that belong to B. 然后,每个B都有一个展开/切换按钮,以显示属于B的C个项目。

What is happening: I click to search, all A are shown. 发生了什么:我单击搜索,所有A都显示出来。 I click on the expand to show the B items of one A object, they are shown. 我单击扩展以显示一个A对象的B项,它们被显示。 I click B to hide it, it hides and then show again. 我单击B将其隐藏,它会隐藏,然后再次显示。 If I click it one more time, it hides, shows and hide. 如果再单击一次,它将隐藏,显示并隐藏。 So it is like every ajax request is including the javascript code and running it. 因此,就像每个ajax请求都包含JavaScript代码并运行它一样。

I think this is pretty much an organization problem, I am not knowing how to include/require and insert the js correctly. 我认为这几乎是一个组织问题,我不知道如何正确地包含/要求和插入js。 I've been trying to solve this issue since yesterday and I think I've seen the code so many times that I can't think out of the box. 自昨天以来,我一直在尝试解决此问题,我认为我已经看过很多次代码了,以至于我想不出来。

So here's some of the organization (I changed the names because there are some business rules): 这是一些组织(我更改了名称,因为有一些业务规则):

SearchResults.php -> Declares a class that has static methods to print out HTML of each item A, B and C and some other helper methods. SearchResults.php->声明一个类,该类具有用于打印A,B和C项以及其他一些帮助器方法的HTML的静态方法。 To make stuff "work", it has a require "js.php"; 为了使东西“工作”,它需要一个“ js.php”; otherwise the A expand button does not work because it does not exist at the time the js is executed and no function is bound. 否则,A扩展按钮将不起作用,因为在执行js时没有该按钮,并且未绑定任何函数。

search.php -> the HTML form with all the search options, nothing important. search.php->具有所有搜索选项的HTML表单,没什么重要的。

js.php -> the javascript stuff, why is it in a ".php"? js.php-> javascript内容,为什么在“ .php”中? I can't even remember, but I think it is because with the php I can require/include: 我什至不记得了,但是我认为这是因为有了php,我可以要求/包含:

<script type="text/javascript" src="../util/js/jquery/1.8.1/jquery.min.js"></script>

<script>

var jQ = jQuery.noConflict(true);

jQ(document).ready(function() {

  jQ(".loadBfromA").click( function(e) {

    if (div.style.display == 'none')
       alert("was hidden, now is showing");
       //call ajax_B.php
       //response is put into the div using .html(data)

    else
       alert("was showing, now is hidden");
    ...

ajax_B.php -> the ajax that access the database and echoes html code that will be put into the A items div. ajax_B.php->访问数据库并回显将放入A项目div中的html代码的ajax。 Here I have to require SearchResults.php, because I call some methods of the class. 在这里,我需要SearchResults.php,因为我调用了该类的某些方法。

Why is it including the jQ(document).ready being executed multiple times? 为什么包括jQ(document).ready被多次执行? How can I fix it? 我该如何解决? Is there any way I can reorganize the code? 有什么办法可以重新组织代码?

Is the ajax_B.php, when requiring SearchResults.php, including the js again because SearchResults.php requires js.php? 因为SearchResults.php需要js.php时,需要SearchResults.php并再次包括js时,ajax_B.php是吗? Does this gets echoed and then put into the div? 这会得到回应,然后放入div吗?

I can't make a fiddle of this because there is ajax included. 我不能摆弄这个,因为其中包括ajax。

Edit: 编辑:

I have tried unbind("click").bind("click", ()) and it didn't work. 我试过unbind(“ click”)。bind(“ click”,()),但没有用。

It looks like the event is being bound multiple times on jQ(".loadBfromA") 看来事件在jQ(".loadBfromA")上绑定了多次

I know this is not the cleanest solution out there, but you could rewrite the actual binding: 我知道这不是最干净的解决方案,但是您可以重写实际的绑定:

jQ(".loadBfromA").bind('click.loadbfroma', function(e) {
    // Do your code
    $(this).unbind('click.loadbfroma');
});

That way you can at least be sure that only one event is bound at all times, no matter how many times the code snippet is being included. 这样,无论代码片段被包含多少次,您至少可以始终确保仅绑定一个事件。 I know this doesn't really help you with the underlying issue but it's a start. 我知道这并不能真正帮助您解决潜在问题,但这只是一个开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM