网络编程 
首页 > 网络编程 > 浏览文章

JS 实现获取打开一个界面中输入的值

(编辑:jimmy 日期: 2026/5/7 浏览:3 次 )

需求
在一个界面中打开另一个界面,通过JS获取在另一个界面中用户输入的值。
示例:
Index.html

复制代码 代码如下:
   <html>

   <head>

     <meta http-equiv="content-type" content="text/html; charset=gbk">

         <title>主页</title>

        <script type="text/javascript">

            function EntryPoint() {

                var style = 'dialogHeight:600px;dialogWidth:800px;status:no;help:0;scrool:yes';

                var a = window.showModalDialog('other.html', '', style);

      

               if (a == undefined) {

                   a = window.returnValue;

               }

              // debugger;

               if (a != null && a.length > 0) {

                   document.getElementById("name").value = a[0];

                   document.getElementById("age").value = a[1];

               }

           }

    </script>

    </head>

    <body>

    <input type="button" value="调用" onclick="EntryPoint()"/><br/>

    <input type="text" name="name" id="name" /><br/>

    <input type="text" name="age" id="age" />

    </body>

    </html> 


另一个界面:

other.html

复制代码 代码如下:
  <html>

     <head>

         <title>操作界面</title>

         

         <meta http-equiv="content-type" content="text/html; charset=gbk">

         

        <script type="text/javascript">

            function postValue() {

                var name = document.getElementById("name").value;

               var age = document.getElementById("age").value;

               var a = new Array();

               a[0] = name;

               a[1] = age;

               //debugger;

               if (window.opener != undefined) {

                   //for chrome

                   window.opener.returnValue = a;

               }

               else {

                   window.returnValue = a;

               }

               window.close();

           }

    </script>

    </head>

    <body>

    <input type="button" value="确定" onclick="postValue();"/><br/>

    名字:<input type="text" name="name" id="name" /><br/>

    年龄:<input type="text" name="age" id="age" />

    </body>

    </html>


在该DEMO中遇到一个问题,那就是chrome中window.close()方法不起作用。最后通过,window.opener来解决chrome和IE的冲突。

上一篇:Javascript Throttle & Debounce应用介绍
下一篇:打开新窗口关闭当前页面不弹出关闭提示js代码
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 站点导航 SiteMap