分享

模态窗体传值

 浮石 2010-03-12
经常遇到这样的问题,需要打开一个窗口,在窗口里面进行操作,然后传值到母窗体,可以用模态窗体传值实现,主要用到的是showModalDialog这个方法
基本介绍:
         showModalDialog()                              (IE 4+ 支持)
         showModelessDialog()                         (IE 5+ 支持)
         window.showModalDialog()                 方法用来创建一个显示HTML内容的模态对话框。
         window.showModelessDialog()            方法用来创建一个显示HTML内容的非模态对话框。
使用方法:
         vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])
         vReturnValue = window.showModelessDialog(sURL [, vArguments] [,sFeatures])
参数说明:
        sURL                --   必选参数,类型:字符串。用来指定对话框要显示的文档的URL。
        vArguments   --    可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。
        sFeatures       --    可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。
----------------
1.   dialogHeight:   对话框高度,不小于100px
2.   dialogWidth:   对话框宽度。
3.   dialogLeft:    离屏幕左的距离。
4.   dialogTop:    离屏幕上的距离。
5.   center:         { yes | no | 1 | 0 } :             是否居中,默认yes,但仍可以指定高度和宽度。
6.   help:            {yes | no | 1 | 0 }:               是否显示帮助按钮,默认yes。
7.   resizable:      {yes | no | 1 | 0 } [IE5+]:    是否可被改变大小。默认no。
8.   status:         {yes | no | 1 | 0 } [IE5+]:     是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
9.   scroll:           { yes | no | 1 | 0 | on | off }:是否显示滚动条。默认为yes。

从网上找了个蛮好的例子,parent.html为母窗体,test1.html为子窗体,点击motai按钮返回文本框的值
 
parent.html
程序代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<title>parent</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript">
function testModelDialog()
{
    var obj=new Object();
    obj.name = self.document.getElementById("txtUserName").value;
    obj.password = self.document.getElementById("txtPWD").value;
    var ddd = window.showModalDialog("test1.html",obj);
   //test1.html 用来指定对话框要显示的html的URL
   //obj 用来向对话框传递参数
    document.getElementById("txtUserName").value =ddd;
}
</script>
</head> <BODY>
<form name = "form">
<input type="text" id="txtUserName">
<input type="text" id="txtPWD">
<input type="button" id="btnSubmit" value="提交" onclick=testModelDialog()>
</form> 
</BODY>
</HTML>
 
子窗体test1.html
程序代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<title>child</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script language="javascript">
function returnalue()
{
var obj = new Object();
obj=window.dialogArguments;
document.getElementById("txtValue").value=obj.name;
}
function backValue()
{
    var re = document.getElementById("txtValue").value;
    window.returnValue = re;
    window.close();
}
</script>
</head> <BODY onload=returnalue()>
<form name = "form">
<input type="text" id="txtValue">
<input type="button" id="btnResult" value="提交" onclick=returnalue()>
<input type="button" id="btnModal" value="motai" onclick=backValue()>
</form> 
</BODY>
</HTML>
虽然这样可以实现了两个网页间的参数传递,但在画面跳转时是却不能调用与html相对应的方法(seasar中的page方法,或ssh中的action方法等)。但是很多时候页面跳转的时候是需要调后台的方法来进行一系列的操作,在子页面初始化某些通过参数进行数据库查询等操作之后的数据,这样需要调用后台方法时候可以选择用ajax去调用后台的方法。至于ajax的具体实现方法参见上一篇“teeda的ajax”。(这是我目前用的方法,虽然可以实现,显然不够完美,如果有更好的方法,还希望朋友们给留点意见哦。)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多