js赋值问题

$("#autocomplete").focus(function() {
    $.ajax({
        type: 'POST',
        url: "__URL__/dishUnit",
        dataType: "html",
        success: function(data) {
            //将var   countries=XX放在这里时,下面函数里的countries就得不到值。
            //不知什么问题
        },
    });

});


var countries = [{
        value: '个'
    },
    {
        value: '碗'
    }
];

$('#autocomplete').autocomplete({
    lookup: countries,
    minChars: 0, //自动完成激活之前填入的最小字符
    // max: 12, 列表里的条目数
});
阅读 3.6k
2 个回答

应该是ajax的异步问题,同步就好了。。

Not sure what do you mean. If I understood it correctly:

If you put var countries = xxx inside the success callback, you're declaring a local variable within the scope of that function, which means you cannot access that variable outside that specific function.

You may read http://www.cnblogs.com/rainman/archive/2009/04/28/1445687.html for more about variable scoping in ECMAScript.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题