js赋值问题

xxx织梦者
  • 1.9k
$("#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, 列表里的条目数
});
回复
阅读 2.9k
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.

你知道吗?

宣传栏