JS 實現擷取開啟一個介面中輸入的值

來源:互聯網
上載者:User

需求
在一個介面中開啟另一個介面,通過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的衝突。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.