2013-07-09 295 views
2

我試圖從父窗口傳遞一些數據以在HTML中彈出窗口。如何將數據從父窗口傳遞到彈出窗口

下面是我的代碼 -

<html> 
<head> 
<script type="text/javascript"> 
function init() 
{ 
    popupWin = window.open('','popupWin',''); 
    popupWin.document.writeln('<html><head><title>test</title></head><body><form><input type="text" id="popupTextBox"/></form></body></html>'); 
    popupWin.document.close(); 
    popupText = popupWin.document.getElementById("popupTextBox"); 
    parentText = document.getElementById("parentTextBox"); 
} 
function transferText() 
{ 
    popupText.value = parentText.value 
} 
</script> 
</head> 
<body> 
<input type="text" id="parentTextBox"/> 
<input type="button" onclick="init();"/> 
</body> 
</html> 

但不知何故,我不能來傳遞文本數據與上面的代碼來彈出窗口。這有什麼問題嗎?

一般來說,我試圖將一些數據從父窗口傳遞到彈出窗口。

+0

這可能有助於http://stackoverflow.com/questions/5187510/how-can-a-javascript- parent-window-send-data-to-popup-window – Seano666

回答

2

你忘了打電話給transferText()
調用transferText()文本轉移後...

<html> 
<head> 
<script type="text/javascript"> 
function init() 
{ 
    popupWin = window.open('','popupWin',''); 
    popupWin.document.writeln('<html><head><title>test</title></head><body><form><input type="text" id="popupTextBox"/></form></body></html>'); 
    popupWin.document.close(); 
    popupText = popupWin.document.getElementById("popupTextBox"); 
    parentText = document.getElementById("parentTextBox"); 
    transferText(); 
} 
function transferText() 
{ 
    popupText.value = parentText.value 
} 
</script> 
</head> 
<body> 
<input type="text" id="parentTextBox"/> 
<input type="button" onclick="init();"/> 
</body> 
</html> 
+1

調用方法的實際流程是什麼?你能提供完整的語法代碼嗎?謝謝 – ferhan

+0

(我將我的代碼添加到答案中) –

+2

有什麼辦法可以做相反的事嗎?將數據從彈出窗口傳遞到父窗口? –

相關問題