将输入从window.open传递到父页面 [英] passing input from window.open to parent page
问题描述
我创建了一个框架集,其中的一个框架使用window.open打开一个弹出窗口.在打开的窗口中,我有一个表单来收集用户的输入.我试图返回到父级但失败的输入.任何帮助. 我尝试通过以下解决方案使用window.opener:弹出窗口返回收盘时将数据发送给父项 但是无法将getChoice()结果返回到父页面.
I have created a frameset, in which one of its frames open a pop-up using window.open . In that window opened i have a form to collect input from users. Input which i'm trying to return to the parent but failing to. Any help. I tried using window.opener using this solution :Popup window to return data to parent on close but failed to get the getChoice() outcome back to the parent page.
框架:
<form>
<button type="button" id="opener" onClick="lapOptionWindow('form.html','', '400','200');">Opinion </button>
</form>
框架js:
function lapOptionWindow(url, title, w, h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
return window.open(url, title,'toolbar=no,location=no,directories=no, status=no, menubar=no, scrollbars=no,resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}
form.html(打开的窗口):
form.html (the window opened):
<div id="lightbox">
<strong>Chinese/Portuguese: Chinese<input type="radio" name="chorpor" value="" id="choice1"> Portuguese<input type="radio" name="chororpor" value="" id="choice2"></strong>
</br><button type="button" id="food" onclick="getChoice()">Submit</button>
</div>
form.js:
function getChoice() {
var choice = "";
var choice1 = document.getElementById("choice1 ");
choice = choice1.checked == true ? "Chinese" : "Portuguese";
return choice;
}
推荐答案
这是一个建议(我更改了一些小东西,请注意):
Here is a proposal (I changed small things, please be careful):
parent.html
的正文:
<button type="button" onclick="popup('popup.html', '', 400, 200);">Opinion</button>
=>
<span id="choiceDisplay">No choice.</span>
parent.html
的JavaScript:
JavaScript of parent.html
:
function popup(url, title, width, height) {
var left = (screen.width/2) - (width/2);
var top = (screen.height/2) - (height/2);
var options = '';
options += 'toolbar=no,location=no,directories=no,status=no';
options += ',menubar=no,scrollbars=no,resizable=no,copyhistory=no';
options += ',width=' + width;
options += ',height=' + height;
options += ',top=' + top;
options += ',left=' + left;
return window.open(url, title, options);
}
function setLang(choice) {
var languages = {
'en': 'English',
'fr': 'French',
'ch': 'Chinese',
'por': 'Portugese'
};
var choiceMessage = 'You have chosen "' + languages[choice] + '".';
document.getElementById('choiceDisplay').innerHTML = choiceMessage;
}
popup.html
的正文:
<form name="f" onsubmit="sendChoiceAndClose()">
<fieldset><legend>Chinese/Portuguese</legend>
<p><input type="radio" name="lang" value="ch" checked>Chinese</p>
<p><input type="radio" name="lang" value="por">Portuguese</p>
<p><input type="submit" /></p>
</fieldset>
</form>
popup.html
的JavaScript:
JavaScript of popup.html
:
function sendChoiceAndClose() {
var form = document.forms['f'];
var choice = form.lang.value;
opener.setLang(choice);
this.close();
}
以下是结果的概述:
因此,为了简要说明一下,在opener
窗口中定义的JavaScript函数从弹出窗口中调用,以发回选择信息.
So to give a short explanation, a JavaScript function defined in the opener
window is called from the pop-up window to send back the choice information.
但是请小心,如果调用的JavaScript包含阻止代码(例如alert(...)
),则仅在阻止代码完成后(例如,在alert
窗口关闭之后),弹出窗口才会关闭.
Be careful though, if the JavaScript called contains blocking code (for instance alert(...)
), the pop-up will be closed only after the blocking code has finished (i.e. after the alert
window has been closed for instance).
这篇关于将输入从window.open传递到父页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!