卡在奇怪的jQuery错误上 [英] Stuck on weird jQuery error

查看:113
本文介绍了卡在奇怪的jQuery错误上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的代码,它存储在一个外部js文件中,并且正确地包含在head部分的主要html中.

Ok, here is my code, it is stored in an external js file, and properly included in the main html in head section.

$(document).ready(function(){
    var checkForConfirmation = function(){
    for(var i=0; i<myOrders.length; i++){
        $.ajax({
            type: "GET",
            url: "orderStatus.php",
            data: {action: 'get', id: myOrders[i]},
            success: function(data){
                if (data){
                    var reply = jQuery.parseJSON(data);
                    $("#fancyAlertLink").fancybox().trigger('click');
                    myOrders.splice(myOrders[i], 1);
                }
            }
        });
        if (myorders.length == 0){
            clearInterval(waitForRestourantInterval);
        }
    }
}
if (myOrders.length > 0){
    var waitForRestourantInterval = setInterval(function(){checkForConfirmation()}, 5000);
}
});

如您所见,当后端脚本("orderStatus.php")获取正确的数据时,我试图显示一个花哨框.

As you can see, I'm trying to display a fancybox when the back-end script ("orderStatus.php"), gets the right data.

如果我不使用jQuery,一切都可以正常工作(例如:window.alert而不是fancybox),但是当我尝试在此函数中使用jQuery时,会出现一个奇怪的错误.

Everything works fine if I don't use jQuery (example: window.alert instead of fancybox), but when I try to use jQuery inside this function, I get a weird error.

Firebug说在$("#fancyAlertLink").fancybox().trigger('click');

Firebug says that there is an error on line $("#fancyAlertLink").fancybox().trigger('click');

没有错误描述,只是"$("

There is no error description, just"$("

我在做什么错??

对不起.我知道这不是一个答案,但我不能在评论中全部提及. 这是更新的"代码. 错误现在消失了,但是fancybox仍然不会从我的脚本中触发.

Sorry. I know this is not an answer, but I can't put it all in comment. Here is the "updated" code. Error is gone now, but fancybox still will not trigger from my script.

编辑:在成功功能中,触发器不起作用.我试图将其移动到外面,并且它可以正常工作.问题是我真的需要成功的内在因素.我试图将触发器调用移动到单独的函数中,并从成功调用该函数,但结果相同.不起作用!有什么建议吗?

The trigger doesn't work when inside the success function. I tried to move it outside and it works. The problem is I really need it inside the success. I tried to move the trigger invoke in separate function and call the function from success, but same result. Doesn't work! Any suggestions?

confirmationDaemon.js

confirmationDaemon.js

$(document).ready(function(){
var checkForConfirmation = function(){
    for(var i=0; i<myOrders.length; i++){
        $.ajax({
            type: "GET",
            url: "orderStatus.php",
            data: {action: "get", id: myOrders[i]},
            context: i,
            success: function(data){
                if(data!="null"){
                    var reply = jQuery.parseJSON(data);
                    $("#fancyAlertLink").trigger("click");
                    myOrders.splice(this, 1);
                }
            }
        });
        if (myOrders.length == 0){
            clearInterval(waitForRestourantInterval);
        }
    }
}
if (myOrders.length>0){
    var waitForRestourantInterval = setInterval(function(){checkForConfirmation()}, 5000);
}
});

主要html文件:(本文中忽略了"smarty + html","smarty {literal}"标记)

main html file: (smarty+html, smarty {literal} tags ignored in this post)

<html>
    <head>
        <script src="jquery-1.4.4.min.js" type="text/javascript"></script>
        <script src="fancyBox/jquery.fancybox-1.3.4.pack.js" type="text/javascript"></script>
        <link rel="stylesheet" href="fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
        <script type="text/javascript">
            var myOrders = new Array();
            {foreach from=$smarty.session.my_orders item=id}
                myOrders.push({$id}):
            {/foreach}
        </script>
        <script type="text/javascript">
            $(document).ready(function{
                $("#fancyAlertLink").fancybox();
            });
        </script>
        <script src="confirmationDaemon.js" type="text/javascript"></script>
    </head>
    <body>
        --- some content here ---
        <a id="fancyAlertLink" href="#fancyAlert">Show fancy</a>
        <div style="display:none">
            <div id="fancyAlert">Fancybox hell yeah!!!</div>
        </div>
    </body>
</html>

设置间隔,AJAX可以正常工作.当我单击显示花式"链接时,将显示花式框.但是它不会从外部js触发.我调试了.它应该工作,执行该行,但是什么也没出现

Set intervals and AJAX work as expected. The fancybox shows when I click on the "Show fancy" link. But it doesn't get triggered from the external js. I debugged it. It should work, it executes that line, but nothing appears

推荐答案

编辑:可能存在一些问题.

There's likely a few problems.

  • 您正在将myOrders[i]传递给.splice().我认为它不包含.splice()要求的索引号.

  • You're passing myOrders[i] to .splice(). I assume it doesn't contain an index number like .splice() requires.

下面描述的i问题的值.

即使解决了这两个问题,您仍然会遇到问题,因为.splice() 修改 该数组,因此任何大于使用的索引号的索引号在接头中已过时,因为您的.splice()正在从阵列中 移除 项.

Even with those two problems resolved, you still have an issue because .splice() modifies the Array, so any index number higher than one that was used in the splice is obsolete, because your .splice() is removing items from the Array.

您正在做

myOrders.splice(myOrders[i], 1);

在对异步AJAX调用的success:回调中.在此代码触发时,ilength的值相同,因此该索引处没有任何项目.

in a success: callback to an asynchronous AJAX call. By the time this code fires, i is the same value of length, so there's no item at that index.

换句话说,最后一项是length - 1,但是i == length也是myOrders[i] == undefined.

In other words the last item is length - 1, but i == length so myOrders[i] == undefined.

一种简单有效的解决方法是将myOrders[i]设置为AJAX调用的context参数.

One simple and efficient fix would be to set myOrders[i] as the context parameter of the AJAX call.

    $.ajax({
        type: "GET",
        url: "orderStatus.php",
        context: myOrders[i],

然后在success:回调中,this将引用该项目.

Then in the success: callback, this will refer to that item.

       success: function(data){
            if (data){
                var reply = jQuery.parseJSON(data);
                $("#fancyAlertLink").fancybox().trigger('click');

                 //  -------------v-----references the proper item
                myOrders.splice( this, 1);
            }
        }

这篇关于卡在奇怪的jQuery错误上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆