2013-01-20 72 views
1

我想从弹出窗口调用父页面上的函数。我不断收到错误Object doesn't support property or method 'GetValueFromChild'.我相信错误是在弹出窗口中的这一行 - window.top.opener.parent.Xrm.Page.GetValueFromChild(person);。我尝试使用window.opener.GetValueFromChild(person);但仍然得到相同的错误。任何帮助深表感谢。这里的代码 -从子窗口调用父窗口函数

//parent 
    $(document).ready(function() { 
     // This needs to be called from Child window 
     function GetValueFromChild(person) { 
      alert(person.Id); 
      alert(person.Name); 
      alert(person.Market); 
     }  
    }); 


//parent - jqgrid 

    colModel: [ 
          { 
           name: 'Person', index: 'PersonName', width: 70, editable: true, edittype: 'button', 
           editoptions: { 
            value: 'Select', 
            dataEvents: [{ 
             type: 'click', 
             fn: function (elem) { 
              var left = (screen.width/2) - (700/2); 
              var top = (screen.height/2) - (550/2); 

              var popup = window.open("popup.htm", "popup", "resizable=1,copyhistory=0,menubar=0,width=700,height=550,left='+left+',top='+top"); 
              popup.focus(); 
             } 
            }] 
           } 
          }, 

//弹出窗口。这个页面有另一个jqgrid和一个OK按钮。

$('#btnOK').click(function() { 
         var person= { 
          Id: grid.jqGrid('getCell', grid.getGridParam('selrow'), 'Id'), 
          Name: grid.jqGrid('getCell', grid.getGridParam('selrow'), 'Name'), 
          Market: grid.jqGrid('getCell', grid.getGridParam('selrow'), 'Market') 
         }; 

         window.top.opener.parent.Xrm.Page.GetValueFromChild(person); //Error is on this line. 
         window.close(); 
        }); 

回答

1

GetValueFromChild范围限定为您ready回调。如果它不需要访问其他作用域函数和变量,那么只需在顶层声明它。如果确实需要访问它们,则需要在回调中为其创建全局引用。

  1. 申报在顶层:

    从范围
    // This needs to be called from Child window 
    function GetValueFromChild(person) { 
        alert(person.Id); 
        alert(person.Name); 
        alert(person.Market); 
    } 
    
  2. 出口:

    $(document).ready(function() { 
        // This needs to be called from Child window 
        function GetValueFromChild(person) { 
         alert(person.Id); 
         alert(person.Name); 
         alert(person.Market); 
        } 
        window.GetValueFromChild = GetValueFromChild; 
    }); 
    
+0

伟大的作品,谢谢!! – tempid

+0

不错,谢谢! – fjckls