简体   繁体   English

将值返回给函数中的函数

[英]return value to function within a function

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

AJAX is asynchronous AJAX是异步的

It appears as though you're making an AJAX request inside getURL . 好像您是在getURL发出AJAX请求一样。

Remember, AJAX requests are asynchronous by default. 请记住,默认情况下AJAX请求是异步的。 That means when you call getURL , the code after it executes immediately instead of waiting for the AJAX response. 这意味着当您调用getURL ,它之后的代码将立即执行而不是等待AJAX​​响应。

Any code that relies on the response needs to be done in the callback to the AJAX request. 任何依赖于响应的代码都需要在AJAX请求的回调中完成。

It appears as though your callback function is doQueryResults . 好像您的回调函数是doQueryResults Place the alert() in that function, and it should fire. alert()放在该函数中,它将触发。

The inner functions are not activated, so running testOne() doesn't do anything. 内部功能未激活,因此运行testOne()不会执行任何操作。 If you want to run it, use: 如果要运行它,请使用:

  function testOne() {
   (function inside() {
      //some other code
      (function anotherOne() {
          var nurl = getUrl("firstLink");
          alert(nurl);
      })();
   })()
  }
  testOne();

Or even 甚至

function testOne() {
   //some other ocde
   return function inside() {
      //some other code
       return function anotherOne() {
          var nurl = getUrl("firstLink");
          alert(nurl);
      };
   }
}
testOne()()();

but may be you should ask yourself: what's the use? 但也许您应该问自己:有什么用? :D :D

Appears to work for me. 似乎为我工作。 See my Fiddle: http://jsfiddle.net/nBTLB/ 见我的小提琴: http : //jsfiddle.net/nBTLB/

there is no problem with that you function is defined within another one 在另一个函数中定义函数是没有问题的

but it seems that getUrl is defined after testOne maybe it hasn't been defined by the time anotherOne gets called? 但是似乎在testOne之后定义了getUrl,也许在调用anotherOne时尚未定义它?

The only way you can get undefined is to call getUrl with an argument other than "firstLink". 不确定的唯一方法是使用除“ firstLink”以外的参数调用getUrl。 Furthermore, the method getUrl should have an else statement with an alternative return when the provided argument is not "firstLink" 此外,当提供的参数不是“ firstLink”时,方法getUrl应该具有else语句,并带有其他返回值

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

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