function funGetItemText(selectedVal,typeID) {
            var itemText="";
            $.ajax({
                type: "POST",
                url: "/Service/DictionaryService.asmx/GetDictionaryList",
                data: "{PageIndex:0,PageSize:0,name:'',typeid:" + typeID + ",OrderField:''}",
                contentType: "application/json",
                dataType: "json",
                success: function(result) {
                    var json = window["eval"]("(" + result.d + ")");
                    var list = json.ListData;
                    $.each(list, function(i) {
                        var item = list[i];
                        if (item.OrderIndex == selectedVal) {
                            itemText=item.ItemText;
                        }
                    });
                }
            });
            return itemText;
        }为什么funGetItemText的返回值还是为空呢?

解决方案 »

  1.   

    补充说明,item.ItemText不为空。
      

  2.   

    你做個斷點,看下item.ItemText是不是本來就空的(可能哪裡出了問題導致它空了),作用域是沒有問題的
      

  3.   

    异步提交能这么做?
     return itemText;都返回了, 这个还没进呢 success: function(result) {
      

  4.   

    ajax方法异步的。在你return itemText的时候itemText赋值语句itemText=item.ItemText都没有执行。
      

  5.   

    你需要改变赋值的方式,例如把itemText定义成全局的。
      

  6.   


            function funGetItemText(selectedVal,typeID) {
                var itemText="";
                $.ajax({
                    type: "POST",
                    url: "/Service/DictionaryService.asmx/GetDictionaryList",
                    data: "{PageIndex:0,PageSize:0,name:'',typeid:" + typeID + ",OrderField:''}",
                    contentType: "application/json",
                    dataType: "json",
                    success: function(result) {
                        var json = window["eval"]("(" + result.d + ")");
                        var list = json.ListData;
                        $.each(list, function(i) {
                            var item = list[i];
                            if (item.OrderIndex == selectedVal) {
                                itemText=item.ItemText;
                                alert(itemText);//结果不为空
                            }
                        });
                    }
                });
                alert(itemText);//结果显示为空
                return itemText;
            }
      

  7.   


      function funGetItemText(selectedVal,typeID) {
                var itemText="";
                $.ajax({
                    type: "POST",
                    url: "/Service/DictionaryService.asmx/GetDictionaryList",
                    data: "{PageIndex:0,PageSize:0,name:'',typeid:" + typeID + ",OrderField:''}",
                    contentType: "application/json",
                    dataType: "json",
                    success: function(result) {
                        var json = window["eval"]("(" + result.d + ")");
                        var list = json.ListData;
                        $.each(list, function(i) {
                            var item = list[i];
                            if (item.OrderIndex == selectedVal) {
                                itemText=item.ItemText;
                            }
                        });
                        return itemText;
                    }
                });
               
            }
      

  8.   

    哦  没仔细看 是异步的问题  获取函数返回值的时候ajax还没执行改变你的获取值的方式  不要再获取funGetItemText的返回值了在  success中把得到的值赋给全局变量
      

  9.   

    现在里面也没有用,默认会返回一个undefined
    改成同步吧。
      

  10.   

    alert(itemText);//结果不为空
    不为空的原因是在function funGetItemText中的itemText=item.ItemText;等价于var itemText = item.ItemText;因为你在最上面定义的var itemText="";已经出了作用域了。
      

  11.   

    这就是异步的原因,如果是同步的,lz的做法没有任何问题。不过在异步中,只能在success和failure等,接收异步结果的地方使用里面的变量。在return itemText;的时候,很可能itemText;还是空值。所以返回的就是空值了。我是这样解决的,如果非要用这个里面的变量,可以在success中直接使用你要return的变量值。如果这样也不行,那就要想其他办法,看情况而定了。
      

  12.   

    设置async: false, 关掉异步,还是存在该问题,求解。
      

  13.   

    ajax上的东西,你不能一路往下的以为代码执行到了哪里就会有什么样的结果
    最好还是把你所要做的操作放到那个回调函数里面去比较好点
      

  14.   

    js的作用域,这哥们儿的博客写的不错
    http://www.cnblogs.com/kym/archive/2010/01/06/1640043.html
      

  15.   

    function funGetItemText(selectedVal,typeID) {
                var itemText="";
                $.ajax({
                    type: "POST",
                    url: "/Service/DictionaryService.asmx/GetDictionaryList",
                    data: "{PageIndex:0,PageSize:0,name:'',typeid:" + typeID + ",OrderField:''}",
                    contentType: "application/json",
                    dataType: "json",
                    async:false,/////////////肯定要同步执行,这个不用多说
                    error:function(){itemText="发生错误!";},//加error回调,说不定你的动态页出问题了
                    success: function(result) {
                        var json = window["eval"]("(" + result.d + ")");
                        var list = json.ListData;
                        itemText="执行进入success";/////////设置一个值,说不定没有和selectedVal配置的项,这样好知道是否执行进入了success回调
                        $.each(list, function(i) {
                            var item = list[i];
                            if (item.OrderIndex == selectedVal) {
                                itemText=item.ItemText;return false;//匹配后return false终止循环
                            }
                        });
                    }
                });
                return itemText;
            }
      

  16.   

    还有一点,你的data配置好像有点问题,怎么传递了字符串,而不是json对象,你那样传递不会形成pageindex,pagesize等的键值对,如果你键值对没处理好就会出错了//data: "{PageIndex:0,PageSize:0,name:'',typeid:" + typeID + ",OrderField:''}",
    //======>
    data: {PageIndex:0,PageSize:0,name:'',typeid: typeID ,OrderField:''},
      

  17.   


            function funGetItemText(selectedVal,typeID) {
                var itemText="";
                $.ajax({
                    type: "POST",
                    url: "/Service/DictionaryService.asmx/GetDictionaryList",
                    data: "{PageIndex:0,PageSize:0,name:'',typeid:" + typeID + ",OrderField:''}",
                    contentType: "application/json",
                    dataType: "json",
                    async: false,          
                    success: function(result) {
                        var json = window["eval"]("(" + result.d + ")");
                        var list = json.ListData;
                        $.each(list, function(i) {
                            var item = list[i];
                            if (item.OrderIndex == selectedVal) {
                                itemText=item.ItemText;
                                alert(itemText);//经测试,显示为预期结果,所以ajax应该是和后 台交互成功了。
                            }
                        });
                    }
                });
                alert(itemText)//测试显示为空,我的问题在这里,为什么会为空呢,而不是ajax与后台交互后返回的结果?
                return itemText;
            }
      

  18.   

    function funGetItemText(selectedVal,typeID) {
                var itemText="";
                $.ajax({
                    type: "POST",
                    url: "/Service/DictionaryService.asmx/GetDictionaryList",
                    data: "{PageIndex:0,PageSize:0,name:'',typeid:" + typeID + ",OrderField:''}",
                    contentType: "application/json",
                    dataType: "json",
                    async: false,          
                    success: function(result) {
                        var json = window["eval"]("(" + result.d + ")");
                        var list = json.ListData;
                        $.each(list, function(i) {
                            var item = list[i];
                            if (item.OrderIndex == selectedVal) {
                                itemText=item.ItemText;
                                alert(i+"==="+itemText);/////////
                            }
                        });
                    }
                });
                alert(itemText+"---return")/////////
                return itemText;
            }你改成这样试试,设置为同步后应该会先输出alert(i+"==="+itemText);/////////,最后才是alert(itemText+"---return")/////////怀疑可能有多个匹配有空值的或者没有同步执行
      

  19.   

    问题解决了,谢谢大家,特别是showbo的热心帮助。
                            
    if (item.OrderIndex == selectedVal){
    itemText=item.ItemText;
    return false;//匹配后return false终止循环
    结贴,哈哈!