2011-05-19 188 views
0

嘗試從getUrl函數獲取返回值,但返回值爲undefined。
我將不勝感激任何幫助。函數返回值返回undefined

感謝
下面是代碼:

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; 
      }   
     } 
    } 
} 
+0

你有一個具體的問題,但你粘貼了一大塊代碼。請儘量重現,以便更快得到正確答案。這也將幫助你自己調試它更容易 – 2011-05-19 16:23:13

回答

2

要返回內部功能,但是從功能的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; 
} 
+0

它返回funcion代碼的所有代碼doQueryResults(r)我希望它返回url = url.nodeValue;謝謝 – Noe 2011-05-19 16:09:33

+0

得到它的工作http://stackoverflow.com/questions/6060902/return-value-to-function-within-a-function – Noe 2011-05-19 16:22:56

0

我認爲你在getUrl方法中做了一個ajax請求,doQueryResults是處理響應的ajax方法的回調。

問題是在這裏,ajax調用是異步進行的,javascript不會等待完全執行,然後繼續執行以執行函數中提到的其他代碼。

在你的代碼

,在那裏你調用的getURL

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

你需要使用Ajax回調使用響應。下面的代碼可以幫助你

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); 
} 
0

js函數將不會從for循環返回任何東西,你需要初始化一個臨時的載體,

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

做這樣

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