JavaScript 弹出子窗体并返回结果到父窗体的实现代码
思路:用window.showModalDialog方法获取到弹出子窗体的引用,再在子页面用window.returnValue="***"来返回结果。
示例代码:(用jQuery简化实现)
父页面:parent.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>父页面</title>
<mce:script language="javascript"><!--
function showmodal()
{
var strReturn = window.showModalDialog("son.html",null,"dialogWidth:800px;dialogHeight:600px;help:no;status:no");
var s="您选择了:";
for(var i=0;i<strReturn.length;i++)
{
s+=strReturn[i]+",";
}
alert(s);
}
// --></mce:script>
</body>
</html>
子页面 son.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>子窗体</title>
<mce:script type="text/javascript" src="jquery-1.4.2.min.js" mce_src="jquery-1.4.2.min.js"></mce:script>
<mce:script type="text/javascript"><!--
var result;
$(function(){
$("#send").click(function(){
var result=new Array();
$("[name=a]:checkbox:checked").each(function(){
result.push($(this).val());
});
window.returnValue=result;
window.close();
});
});
// --></mce:script>
</head>
<body>
<p>
<input type="checkbox" name="a" value="苹果" />苹果
<input type="checkbox" name="a" value="橘子" />橘子
<input type="checkbox" name="a" value="香蕉" />香蕉
<INPUT type="button" value="提交" id="send" />
</p>
</body>
</html>
总结:
以上是 JavaScript 弹出子窗体并返回结果到父窗体的实现代码 的全部内容, 来源链接: utcz.com/z/340361.html