`

模式对话框父子窗口间的通信

JSP 
阅读更多
   本文主要对防止模式对话框弹出新子窗口,和父子窗口间的通信进行介绍。
   比如,如下代码是子窗口(模式窗口)的jsp中的js代码。
  function test() {
var flag = document.getElementById("key");
if (flag.value == "true") {
     window.returnValue = true;
window.close(); }
}

"key"是jsp页面中某标签的id,比如其可以是<s:hidden name="key" value="value1">,其中value1是action中的某个返回属性,当value1=true时,子窗口就向父窗口返回true并关闭该子窗口(调用window.close();).
  上面光给出了子窗口中的js代码,下面给出子窗口中的jsp代码,比如jsp代码为:
<s:form action="test" target="heihei">
<s:hidden name="key" value="%{value1}"/>
..............................
</s:form>
<script language="javascript" type="text/javascript">
<!--
    window.name='heihei';
test();
//-->
</script>
上面<script language="javascript" type="text/javascript"></script>
中的window.name=“heihei”;就是防止子窗口重新再另外打开一个子窗口的。并且window.name="heihei"中的"heihei"要和表单<s:form action="test" target="heihei">中的target的值(”heihei“)相同,简而言之就是,每次打开的子窗口都是当前窗口,即是在target指定的窗口中打开。要实现在模态子窗口中传值到父窗口,需要使用window.returnValue完成
     在父窗口中就可以得到该子窗口返回的值,其得到方式为:
     var newWin=window.showModelDialog(url,window,'');当上面的子窗口返回为ture是,父窗口中的值newWin的值就为true否则为false。函数window.showModelDialog(url,window,'')中的第一个参数可以是一个action(比如:test.action或"test.shtml?page=1"),也可以是一个具体的jsp(test.jsp)页面。
   1. 在子窗口中:

//获取父窗口某字段值,对该值加一后返回父窗口
var parent=window.dialogArguments;
var x=parent.docuement.getElementById("age").value;
x=x+1;

//传回x值
window.returnValue=x;

   2.在父窗口中:

//获取来自子窗口的值,并把其赋给某个对象
var newWin=window.showModelDialog(url,window,'');
if(newWin!=null)
document.getElementById("age").value=newWin;
   3.子窗口设置父窗口的值使用方法如下:

     子窗口中:
//age是父窗口中的某标签对象的id

var parent=window.dialogArguments;
var x=parent.document.getElementById("age").value;
x=x+1;
//设置父窗口中age属性值
parent.document.getElementById("age").value=x;

走笔至此!

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics