JS (window.showModalDialog) 弹窗中值的获取方式!

先来看下具体的实现效果!
这里写图片描述

如何应用到实际的项目中也是仁者见仁智者见智的问题啦!

主窗口

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>主窗口</title>
<script type="text/javascript">
    var dialogShow = function (){
    //设置模式窗口的一些状态值  
            var windowStatus = 'dialogWidth:260px;dialogHeight:180px;center:yes;status:0;';  
            //在模式窗口中打开的页面  
            var url = "child.html";  
            var obj = document.parentForm; //指定页面对象  
            var returnValue = window.showModalDialog(url ,obj,windowStatus); //方法返回值  
            alert(returnValue[0]);
    }
</script>
</head>
<body>
    <form name="parentForm">
        姓名:<input type='text' name='textValue1'><input type="button" value="请选择" onclick="dialogShow()" /><br/>
        年龄:<input type='text' name='textValue2'><br/>
    </form>
</body>
</html>

弹出窗口

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>弹出窗口</title>
<script type="text/javascript">
function retrunValue(){  
     var inputValue1 = document.all.inputValue1.value;// 获取输入的值  
     var inputValue2 = document.all.inputValue2.value; 

     var obj = window.dialogArguments; //获取父页面对象 
     obj.elements["textValue1"].value = inputValue1; //给父页面对象赋值  
     obj.elements["textValue2"].value = inputValue2;  

     var inputValues = [];//存到数组中
     inputValues.push(inputValue1);//添加 
     inputValues.push(inputValue2);//添加

     window.returnValue = inputValues; //方法返回值
     window.close(); //关闭窗口 
}
</script>
</head>
<body>
    姓名:<input type="text" name="inputValue1"><br/>  
    年龄:<input type="text" name="inputValue2"><br/>  
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="确定" onclick="retrunValue()">  
</body>
</html>

本人菜鸟一枚,如有错误之处还望指出,谢谢!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值