Yii的afterAjaxUpdate发生了13次? [英] Yii afterAjaxUpdate happening 13 times?
问题描述
在我CGridView我有这个简单的功能:
in my CGridView I have this simple function:
'afterAjaxUpdate' =>
'function(id, data) {
var checks2 = $("#checks").val().split(",").sort();
$("#rule-competitors-grid input:checkbox").each(function() {
console.log($.inArray($(this).attr("name").substr(11,$(this).attr("name").length - 12), checks2));
if ($.inArray($(this).attr("name").substr(11,$(this).attr("name").length - 12), checks2) !== -1)
$(this).attr("checked", "checked");
});
}',
在一个列表,其中显示2项的时间(调试)。
On a list that shows 2 items at a time (for debugging).
见的console.log()
在那边?他们出现13次,每次通话。
我该如何解决这个问题?
See the console.log()
s over there? They happen 13 times each call.
How can I fix this?
推荐答案
该解决方案是愚蠢和不相关的,但我敢肯定有人在这个世界上可能会遇到相同的。
The solution was dumb and unrelated, but I'm sure someone in this world might encounter the same.
我的问题是,JavaScript是通过AJAX加载在一个对话框/选项卡中加载的页面,而不是在主页面。什么情况是,点击触发被重新分配,而添加应用于元素每次我重新加载标签/对话时间。重装它13次检查的微小变化(因为它是一个AJAX对话框我能够避免到更新整个页面)后,它被装进去。
My problem was that the JavaScript was loaded through AJAX in the loaded page in a dialog/tab and not in the main page. What happens is that the "click" trigger was re-assigned, and added to the elements every time I reloaded the tab/dialog. After reloading it 13 times to check on small changes (since it's an AJAX dialog I was able to avoid having to update the whole page), it was loaded inside.
要避免这种情况,要么尽快取消绑定事件对话框/选项卡将被重新装入,或者使用。对()
热闹的负载它的主要页面,而不是标签/对话框,因此code不重新添加到事件每次加载它。
To avoid this, either unbind the events as soon as the dialog/tab is reloaded, or use .on()
to lively load it on the main page and not the tab/dialog so the code isn't re-added to the event every time you load it.
这篇关于Yii的afterAjaxUpdate发生了13次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!