2013-01-20 69 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