window.opener,是通过window.open打开子窗体的父窗体的引用。
比如在父窗体parentForm里面,通过window.open("subForm.html"),那么在subform.html中window.opener就代表parentForm。既然在子窗体中能够拿到父窗体的引用,那么就可以在子窗体中设置父窗体的字段值或者调用js方法。
实例:添加人员信息时,其中的机构信息通过子窗体完成输入
父亲窗体,用于添加人员信息。
子窗体完成输入后,机构信息(id、name)自动填充到父窗体的orgId、orgName域
[img]http://files.jb51.net/file_images/article/201308/201308191558234.gif?2013719155842[/img]
html代码
[url=org.do?parentId=${org.id }&select=true]<tdalign="center" vAlign="center">${org.sn }</td>
<tdalign="center" vAlign="center">${org.parent.name}</td>
</tr>
</c:forEach>
</c:if>
JS代码
functionselectOrg(id,name){
if(window.opener){
window.opener.document.all.orgIdId.value= id;
window.opener.document.all.orgNameId.value= name;
window.close();
}
}
选择机构信息后的结果
[img]http://files.jb51.net/file_images/article/201308/201308191559466.gif?2013719155957[/img]
完成机构信息(id、name)的输入了,只是id在隐藏域中,看不到而已。
[b]小结[/b]
说到对父窗体的引用,除了window.opener,就是window.parent了。window.opener是用于通过window.open方式打开子窗体,而window.parent是用于通过iframe方式打开子窗体。