子窗口和父窗口进行相互传值

xiaoxiao2026-04-16  4

最近做了一个小小的聊天室的项目,在添加好友的时候用到了window.showmodaldialog() 在A.jsp中的点击链接弹出B.jsp进行添加好友分组类型,并且把添加好友分组类型产生的Id和名字返回到A.jsp中并添加到页面中的下拉菜单中,在网上找了许多资料,但都很散,所以自己整理了一份。 环境(struts+spring+hibernate+dwr) A.jsp(父窗口) <script type="text/javascript"> function addFriendType() { //打开进行好友类型添加的页面进行添加,并获得返回值 var type=window.showModalDialog('system/addFriendType.jsp',window,'dialogwidth:200px;dialogheight:35px;help:0;center:yes;resizable:0;status:0;scroll:yes'); if(type!=undefined)//如果返回的不是undefined就添加到下拉菜单中 { document.getElementById('friendTypeId').options.add(new Option(type[1],type[0])); } } </script> <form action="" name="myform" id="myform"> <br> <input type="hidden" id="userId" value="${user.userId}"> <input type="hidden" id="friendId" value="${friend.userId}"> <table align="center" border="0" cellspacing="0" cellpadding="0"> <tr> <td>好友分组: <select name="friendTypeId" id="friendTypeId"> <logic:notEmpty name="friendTypeList"> <logic:iterate id="friendType" name="friendTypeList"> <option value="${friendType.friendTypeId}">${friendType.friendTypeName}</option> </logic:iterate> </logic:notEmpty> </select> <a href="#" οnclick="addFriendType();">新建分组</a>//单击调用添加好友分组类型的方法 </td> </tr> </table> </form> addFriendType.jsp(子窗口,添加好友类型的页面) <script type="text/javascript"> function addFriendType() { var friendTypeName=document.getElementById('friendTypeName').value; if(friendTypeName=='') { alert('请输入好友分组名称'); } else { //获得父窗体的值,并进行添加 var userId=window.dialogArguments.document.getElementById('userId').value var selectValue=window.dialogArguments.document.getElementById('friendTypeId'); var isExist=false; for(var i=0;i<selectValue.length;i++) { if(selectValue[i].text==friendTypeName) { alert('该分组名称已经存在'); isExist=true; return; } } if(isExist==false) { systemService.addFriendType(userId,friendTypeName,setResult);//用dwr进行好友类型添加并返回添加产生的id在setResult函数中进行处置 } } } function setResult(data) { if(data!=0) { alert('添加好友类型成功'); var type= new Array();//将产生的好友类型id和名称添加进数组中并返回到父窗体 type[0]=data; type[1]=document.getElementById('friendTypeName').value; window.returnValue=type; } else alert('系统升级中,添加失败'); } </script> </head> <body> <center> <br> <input type="text" name="friendTypeName" value="请输入好友分组名称" id="friendTypeName"> <input class="button" type="button" value="添  加" οnclick="addFriendType();">//单击调用添加好友类型的方法 </center> </body> 注:在这里应该注意几点 1.子窗体中的function setResult(data)函数是dwr自带的data是返回的封装对象,可自己命名,但不可去掉 2.将值返回到父窗体中一定要判断返回类型,因为,无论怎样子窗体都会产生返回值的 3.另见http://ajava.org/course/javascript/14008.html处也讲解了子窗体传值给父窗体 问题
转载请注明原文地址: https://www.6miu.com/read-5047486.html

最新回复(0)