弹出窗口中的Texbox值保持为null

Bri*_*ian 6 javascript php jquery popup

我有一个包含这段代码的php页面:

echo '<div id="popup" style="display:none">';
echo '<form id="AddForm" name="AddForm" method="get">';
echo '<table><tr>';
echo '<td>Software Name: </td><td><input type="text" id="SoftwareName"/></td></tr>';
echo '<tr><td>Software Type:</td><td><input type="text" id="SoftwareType"/></td></tr>';
echo '<tr><td>License Method:</td><td><input type="text" id="LicenseMethod"/></td></tr>';
echo '<tr><td><input type="button" value="Add" OnClick="opener.GetAddData();"></td><td></td>';
echo '</tr></table>';
echo '</form>';
echo '</div>';
Run Code Online (Sandbox Code Playgroud)

Buttan调用CreatePopup():

echo "<input type='submit' value='Add' OnClick='CreatePopup();'/>";
Run Code Online (Sandbox Code Playgroud)

我使用以下代码打开此div作为弹出窗口:

function CreatePopup()
{   
       var w = null;
        w = window.open('index.php?List=SoftwareLicenseAllocations', 'test', 'height=125,width=300');
        w.document.write( $("#popup").html());
        w.document.close();
}
Run Code Online (Sandbox Code Playgroud)

从弹出窗口中获取文本框值的代码:

function GetAddData()
{   
    var SoftwareName = document.getElementById('SoftwareName').value;//.getElementById('SoftwareName').value;
    var SoftwareType = document.getElementById('SoftwareType').value;
    var LicenseMethod =document.getElementById('LicenseMethod').value;        

    alert(SoftwareName, SoftwareType, LicenseMethod);       

    AddNew(SoftwareName,SoftwareType,LicenseMethod);

}
Run Code Online (Sandbox Code Playgroud)

截图:

弹出

每当我调用GetAddData()并在弹出框中插入文本并单击按钮时,值仍为null.

为什么会这样?如何获取文本框值?

我正在使用Pear PHP和OpenIT(以及旧资产管理CMS)的修改版本.

小智 1

如果我理解你想要正确做的事情......也许这个?

更改此行(刚刚添加的窗口):

echo '<tr><td><input type="button" value="Add" OnClick="opener.GetAddData(window);"></td><td></td>';
Run Code Online (Sandbox Code Playgroud)

并向GetAddData函数添加一个窗口参数:

function GetAddData(window)
{   
    var popupDoc = window.document;
    var SoftwareName = popupDoc.getElementById('SoftwareName').value;//.getElementById('SoftwareName').value;
    var SoftwareType = popupDoc.getElementById('SoftwareType').value;
    var LicenseMethod = popupDoc.getElementById('LicenseMethod').value;        

    alert(SoftwareName, SoftwareType, LicenseMethod);       

    AddNew(SoftwareName,SoftwareType,LicenseMethod);
}
Run Code Online (Sandbox Code Playgroud)

当您调用opener.GetAddData弹出窗口时,DOM 方法将在opener文档中搜索,而不是在弹出窗口中搜索。您需要将弹出窗口的window对象传递给函数,以便它知道应该在弹出窗口的document.