weixin_33737774 2014-04-02 13:55 采纳率: 0%
浏览 10

为什么ListScores()无法正常运行和调用?

我遵循了我在这里找到的所有建议,直到完成所有jQueryAjax请求,但我一定是错过了一些基本的东西......

我有以下功能:

function ListScores(leaguex, nameid) {
    return $.ajax({
        url: 'sg_ajaxListData.php',
        data: {
            nme: nameid,
            league: leaguex
        },
        dataType: 'json',
        success: function (rows) {
            for (var i in rows) {
                var row = rows[i];
                var score1 = row["score1"];
                var score2 = row["score2"];
                var round = row["round"];
                $('#output').append("<br />Round " + round + " - " + score1);
                if (leaguex == 'r') {
                    $('#output').append(" " + score2);
                }
            }
        }
    });
}

它是从我代码中的document.ready函数中调用的,运行得非常完美,返回数据并按预期显示。我希望在ListScores()显示了它的数据之后再运行另一个函数,因此,在document.readyfunction中的其他地方,我放置了:

$.when(ListScores()).done(function(a1){
console.log("hello",leaguex,nameid, a1);
});

但是控制台日志中没有显示任何内容,该函数似乎从未被调用过。我是不是漏掉了什么明显的东西?

谢谢帮助!

  • 写回答

1条回答 默认 最新

  • weixin_33743703 2014-04-02 14:08
    关注

    Edit : after@ Jason P comment, I think this wrong solution But I will keep it as it shows how to autoinvoke a function ---End of edit

    It is not firing because ListScores() was never invoked

    You should wrap the ListScores() and immediately invoked it.

    function ListScores(){
    
    }
    

    To invoke it

    You should add the following

     (function ListScores(){
    
     })();
    

    Got it?

    评论

报告相同问题?