【问题标题】:passing input from window.open to parent page将输入从 window.open 传递到父页面
【发布时间】:2015-12-13 13:04:28
【问题描述】:

我创建了一个框架集,其中一个框架使用 window.open 打开一个弹出窗口。在打开的那个窗口中,我有一个表单来收集用户的输入。我试图返回给父级但未能返回的输入。任何帮助。 我尝试使用 window.opener 使用这个解决方案:Popup window to return data to parent on close 但未能将 getChoice() 结果返回到父页面。

框架:

<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(打开的窗口):

<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;
    }

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    这里有个建议(我改了小东西,请注意):

    parent.html的正文:

    <button type="button" onclick="popup('popup.html', '', 400, 200);">Opinion</button>
    =&gt;
    <span id="choiceDisplay">No choice.</span>
    

    parent.html的JavaScript:

    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:

    function sendChoiceAndClose() {
      var form = document.forms['f'];
      var choice = form.lang.value;
      opener.setLang(choice);
    
      this.close();
    }
    

    这里是结果的概述:

    所以简单解释一下,在弹出窗口中调用opener 窗口中定义的 JavaScript 函数以发送回选择信息。

    但请注意,如果调用的 JavaScript 包含阻塞代码(例如 alert(...)),则只有在阻塞代码完成后(例如,在 alert 窗口关闭后)才会关闭弹出窗口.

    【讨论】:

    • @dan 你是什么意思?
    • 可以开几分钟的聊天吗?
    • 这不是 StackOverflow 的精神,但如果需要,您可以查看我的个人资料与我联系。
    • @maxime.bochon 这太酷了。我将您的代码粘贴到 2 个文件中,它在第一次尝试时工作(在 Windows 10 上的 Chrome 上)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    相关资源
    最近更新 更多