简体   繁体   English

函数的返回值返回undefined

[英]return value from function returns undefined

Trying to get a return value from getUrl function but it comes back as undefined. 试图从getUrl函数获取返回值,但它返回未定义。
I would appreciate any help. 我将不胜感激任何帮助。

Thanks 谢谢
Here is the code: 这是代码:

    function createXmlFicaRsi(xmlDoc,xmlFileName) {     
    var mystr = "<?xml version='1.0' encoding='utf-8'?><result><rows>"+strStor+"</rows></result>"
    jQuery(document).ready(function(){ 
      jQuery("#fRsiGrid").jqGrid({
        datatype: 'xmlstring',
        datastr : mystr,
        colNames:['Year','Earnings', 'Amt<br/>Needed <br/>1 QC','Amt<br/>Needed <br/>4 QC','#<br/>of<br/> QCs','Monthly<br/>Under FRA','Yearly<br/>Under FRA','Monthly<br/> Yearly of<br/> Attain.<br/> FRA','Year of<br/> Attain. of<br/> FRA','YOC*','Sum of<br/>Post-1977<br/>YOCs'],
        colModel :[ 
            {name:'yearRsi', index:'yearRsi', width:55, resizable:false, align:'center', sorttype:'int'},
            {name:'earnRsi', index:'earnRsi', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'1qcRsi', index:'1qcRsi', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'4qcRsi', index:'4qcRsi', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'numqcRsi', index:'numqcRsi', width:40, resizable:false, align:'right', sortable:false}, 
            {name:'mfra', index:'mfra', width:65, resizable:false, align:'right', sortable:false}, 
            {name:'yfra', index:'yfra', width:65, resizable:false, align:'right', sortable:false},
            {name:'myafra', index:'myafra', width:85, resizable:false, align:'right', sortable:false},
            {name:'yafra', index:'yafra', width:65, resizable:false, align:'right', sortable:false},
            {name:'yoc', index:'yoc', width:65, resizable:false, align:'right', sortable:false},          
            {name:'sumpost', index:'sumpost', width:60, resizable:false, align:'right', sortable:false} ],     
        rowNum:-1,      
        hidegrid: false,
        width: 760, 
        height: 460,
        shrinkToFit: false,         
        caption: '<span id=fRsiGrid_caption>FICA Earnings, QC, AET and YOC amounts after 1977</span>'       
      });     

      $('.ui-jqgrid .ui-th-column').css('height', '40px');
      $('.ui-jqgrid .ui-jqgrid-htable th div').css('height', '40px'); 
      $('.ui-jqgrid-title').css('font-size', '.8em');//Font size for title
      $('.ui-jqgrid .ui-th-column').css('font-size', '.7em');//Font size for header content 
      $('#fRsiGrid_caption').append("<span id='whatLink' style='font-size:large;color:blue;text-decoration:none;cursor:pointer'>*</span>");     

    }); 
    $('#jqgh_1qcRsi').addClass("gridLink");
    $('#jqgh_4qcRsi').addClass("gridLink");
    $('#jqgh_mfra').addClass("gridLink");
    $('#jqgh_yfra').addClass("gridLink");
    $('#jqgh_myafra').addClass("gridLink");
    $('#jqgh_yafra').addClass("gridLink");
    $('#jqgh_yoc').addClass("gridLink");

    $("#jqgh_1qcRsi").click(function() {
        var nurl = getUrl("QueryView-QC");
        alert(nurl);        
    }); 
}

    function getUrl(urlNm){
    DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getUrls', urlNm, doQueryResults);
    function doQueryResults(r){     
        xmlDoc = loadXMLString(r);      
        y = xmlDoc.getElementsByTagName("URL");

        for (i = 0; i < y.length; i++) {            
            url = y[i].attributes.getNamedItem("val").nodeValue;            
            if (url == urlNm)
            {                           
                url = y[i].childNodes[0];
                //alert(url.nodeValue);
                url = url.nodeValue;
                return url;
            }           
        }   
    }
}   

you are returning the interior function but nothing from the getURL function. 你正在返回内部函数但getURL函数没有任何内容。

function getUrl(urlNm){
DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getUrls', urlNm, doQueryResults);
var returnVal = function doQueryResults(r){     
    xmlDoc = loadXMLString(r);      
    y = xmlDoc.getElementsByTagName("URL");

    for (i = 0; i < y.length; i++) {            
        url = y[i].attributes.getNamedItem("val").nodeValue;            
        if (url == urlNm)
        {                           
            url = y[i].childNodes[0];
            //alert(url.nodeValue);
            url = url.nodeValue;
            return url;
        }           
    }   
  }
  return returnVal;
}

js function will not return anything from the for loop, you need to init a temp carrier, js函数不会从for循环返回任何内容,你需要初始化一个临时载体,

function youFunction (){
    for(){
        if(true){
            return value; //wrong
        }
    }
}

Do like this 这样做

function youFunction (){
    var carrier;
    for(){
        if(true){
            carrier = value; 
            return false; //end the loop
        }
    }
    return carrier;
}

I think you are making an ajax request in getUrl method and doQueryResults is the callback to ajax method which handles the response. 我认为你在getUrl方法中发出ajax请求,而doQueryResults是对处理响应的ajax方法的回调。

The problem is here, the ajax call is made asynchronously and javascript does not wait for execute it completely and move on next to execute other code mentioned in function. 问题在于,ajax调用是异步进行的,javascript不等待完全执行它,然后继续执行函数中提到的其他代码。

in your code, where you invoked the getUrl 在您的代码中,您调用了getUrl

var nurl = getUrl("QueryView-QC"); // getUrl will trigger the ajax request and return nothing ie. undefined so the nurl is undefined.

you need to use ajax callback to use response. 你需要使用ajax回调来使用响应。 Following code may help you 以下代码可以帮助您

function getUrl(urlNm){
 DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getUrls', urlNm, doQueryResults);
            function doQueryResults(r){     
                xmlDoc = loadXMLString(r);      
                y = xmlDoc.getElementsByTagName("URL");

                for (i = 0; i < y.length; i++) {            
                    url = y[i].attributes.getNamedItem("val").nodeValue;            
                    if (url == urlNm)
                    {                           
                        url = y[i].childNodes[0];
                        //alert(url.nodeValue);
                        url = url.nodeValue;

                        // perform your stuff with url
                       doWithUrl(url);
                    }           
                }   
            }

// function to handle the url 
function doWithUrl(url){
 alert(url);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM