2011-05-19 4 views
0

Si vous tentez d'obtenir une valeur de retour de la fonction getUrl, celle-ci revient indéfinie.
J'apprécierais toute aide.renvoie la valeur de la fonction renvoie undefined

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

Vous avez un problème spécifique, mais vous avez collé un énorme morceau de code. Veuillez le reproduire au minimum pour obtenir une bonne réponse plus rapidement. Cela vous aidera aussi à le déboguer vous-même plus facilement –

Répondre

2

vous retournez la fonction intérieure, mais rien de la fonction 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

il retourne tout le code du code de fonction doQueryResults (r) Je veux qu'il renvoie url = url.nodeValue; Merci – Noe

+0

Je l'ai eu à travailler http://stackoverflow.com/questions/6060902/return-value-to-function-within-a-function – Noe

0

Je pense que vous faites une demande de paiement ajax dans la méthode getUrl et doQueryResults est le rappel à la méthode ajax qui gère la réponse.

Le problème est ici, l'appel ajax est fait de manière asynchrone et javascript n'attend pas de l'exécuter complètement et de passer ensuite à exécuter d'autres codes mentionnés dans la fonction.

dans votre code où vous le getUrl invoquez

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

vous devez utiliser le rappel ajax utiliser la réponse. code suivant peut vous aider à

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

fonction js ne retournera rien de la boucle, vous devez initialiser un support temporaire,

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

font comme ça

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