首页 > 解决方案 > 动态创建的弹出窗口的问题

问题描述

当用户单击某个按钮时,我正在尝试使用一些输入字段动态创建一个弹出表单;当用户关闭弹出窗口时,我很难返回输入值。

在主窗体上,我有类似的内容:

function GetAnswers() {
    var answers=ShowPrompts();
    return answers; 
}

function ShowPrompts() {
    var answer="";
    var popup=window.open('','PopupForm'); 
    TextControl= popup.document.createElement("INPUT");
    TextControl.setAttribute("type", "text");
    TextControl.value="";
    TextControl.setAttribute("id", 'idInput1');
    popup.document.body.appendChild(TextControl);

    var ButtonControl = popup.document.createElement("button");
    var TextNode = popup.document.createTextNode("Save"); 
    ButtonControl.appendChild(TextNode); 
    ButtonControl.style.float="right";
    popup.document.body.appendChild(ButtonControl);

    ButtonControl.onclick = function(){
        InputControl=popup.document.getElementById('idInput1');
        answer=InputControl.value;  
    }

    return answer;
}

问题是“答案”仅在用户单击“保存”按钮时设置;所以当 ShowPrompts 被调用时,它只返回“”。如何使 GetAnswers 函数等待用户单击弹出窗口上的保存按钮并获取答案值?

标签: javascript

解决方案


推荐阅读