簡體   English   中英

將值返回給函數中的函數

[英]return value to function within a function

試圖從getUrl函數獲取返回值,但返回時未定義。
我將不勝感激任何幫助。

謝謝
這是代碼:

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是異步的

好像您是在getURL發出AJAX請求一樣。

請記住,默認情況下AJAX請求是異步的。 這意味着當您調用getURL ,它之后的代碼將立即執行而不是等待AJAX​​響應。

任何依賴於響應的代碼都需要在AJAX請求的回調中完成。

好像您的回調函數是doQueryResults alert()放在該函數中,它將觸發。

內部功能未激活,因此運行testOne()不會執行任何操作。 如果要運行它,請使用:

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

甚至

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

但也許您應該問自己:有什么用? :D

似乎為我工作。 見我的小提琴: http : //jsfiddle.net/nBTLB/

在另一個函數中定義函數是沒有問題的

但是似乎在testOne之后定義了getUrl,也許在調用anotherOne時尚未定義它?

不確定的唯一方法是使用除“ firstLink”以外的參數調用getUrl。 此外,當提供的參數不是“ firstLink”時,方法getUrl應該具有else語句,並帶有其他返回值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM