1. 程式人生 > >Jquery 頁面間傳值

Jquery 頁面間傳值

實現原理: 實現方式不是很複雜,父頁面A開啟一個子頁面 A1,並同時寫一個帶引數的接收資料函式Receive(result),在A1頁面進行邏輯操作,然後呼叫父頁面A的Receive(result)函式,將結果傳遞到父頁面。

使用場景:資料選擇,引數計算等在多資訊頁面同一個頁面展示影響頁面美觀。

父頁面:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>頁面間通過JS傳值</title>
        <script src="//cdn.bootcss.com/jquery/1.9.0/jquery.js"></script>
        <script type="text/javascript">
 function CC_Click {
 window.open("default.html","SelectUser", "scrollbars=yes,width=600,height=400,left=150,top=100,status=yes,resizable=yes");
 }

 function GetResultFromDefault(result) {
 $("#Result").val(result)
 }
        </script>
    </head>

    <body>
        <div id="Div_Class">

 <button onclick="CC_Click"> click me </button>
 <input type="text" id="Result" value="Nothing"></input>
        </div>

    </body>

</html>

子頁面:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="//cdn.bootcss.com/jquery/1.9.0/jquery.js"></script>
        <script>
 function SendInfo {
 var Resu = $("#info").val;
 if(window.opener == null) {
 window.close;
 return;
 } else {
 window.opener.GetResultFromDefault(Resu);
 window.close;
 }
 }
        </script>
    </head>

    <body>
        <div>
 <input type="text" id="info" />
 <button onclick="SendInfo">Send</button>
        </div>
    </body>

</html>