2009-08-05 64 views
2

我目前有一個從我的應用程序主頁面顯示的Ajax彈出窗口。在這個彈出窗口中,我有選擇列表,並且當用戶關閉彈出窗口時,我想將彈出的選定值傳遞迴主頁面。如何將數據從Ajax彈出窗口傳遞給父頁面?

我覺得有一個基本的方法來做到這一點,但我似乎無法弄清楚。

+1

是什麼「AJAX彈出」是什麼意思?如果它是瀏覽器窗口彈出窗口,則不是ajax。如果它是通過ajax請求填充的JavaScript彈出窗口,則取決於您正在使用的庫/代碼 – 2009-08-05 02:03:10

回答

1

當您打開通過JavaScript的彈出窗口(不管內容是通過AJAX或其他技術),你可以很容易地控制它。

對於您的問題,您需要window.opener指向回原文件: 乾淨的一個:在彈出的代碼

有:

<body onclose="window.opener.somevar=document.getElementById('myid').value;window.opener.orsomefunction('value');"> 
<input id="myid"/> 
</body> 

這樣,當你關閉它設置一個變量和/或調用一個函數。

你也可以將它設置功能按鈕或直接輸入

<button onclick="window.opener....;window.close();"/> 
0
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<?PHP 
$test_1 = $_GET['variable1']; 
$test_2 = $_GET['variable2']; 
$test_3 = $_GET['variable3']; 

$test1 = "blabla1"; 
$test2 = "blabla2"; 
$test3 = "blabla3"; 
?> 
<html> 
<head> 
    <title>Untitled</title> 
    <script type="text/javascript" src="js/mootools-1.2.4-core-yc.js"></script> 
    <script type="text/javascript"> 
    //on dom ready... 
window.addEvent('domready', function() { 

    /* ajax replace element text */ 
    $('ajax-replace').addEvent('click', function(event) { 
     //prevent the page from changing 
     event.stop(); 
     //make the ajax call, replace text 
     var req = new Request.HTML({ 
      method: 'get', 
      url: $('ajax-replace').get('href'), 
      data: { 'variable1' : '<?=$test1?>', 'variable2':'<?=$test2?>', 'variable3' : '<?=$test3?>' }, 
      //onRequest: function() { alert('Request made. Please wait...'); }, 
      update: $('message-here'), 
      onComplete: function(response) { alert('Variable 1: <? echo $test1; ?> Variable 2: <? echo $test2; ?> Variabble 3: <? echo $test3; ?>'); $('message-here'); 
      } 
     }).send(); 
    }); 
}); 

    </script> 
</head> 

<body> 


<p> 
    <a href="index.php" id="ajax-replace">Click</a> 
</p> 
<div id="message-here"> 
<?PHP 
echo $variable_1."<br>"; 
echo $variable_2."<br>"; 
echo $variable_3."<br>"; 
?> 
</div> 

</body> 
</html> 
+1

僅在StackOverflow中才能使用代碼解答。 – 2012-11-25 09:03:48

相關問題