使用jQuery實現頁面跳轉傳值,個人認為使用 session 應該會更簡單,但由於項目的一些原因並沒有使用session,這里使用的是 location.href
第一個頁面獲取值,使用 location.href 跳轉到相應的第二個頁面,由於是使用a標簽跳轉,只能實現跳轉固定頁面。
傳值的過程如果是一個對象或者是數組,需要使用 JSON.string() 對其進行類型轉換;
然后使用 encodeURL 對數據進行轉碼;
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.js"></script> </head> <body> <button id="btn">點擊</button> <script type="text/javascript"> $("#btn").click(function() { var aa = []; var as = {}; as.s = 'ssss'; aa.push(as); var aaS = JSON.stringify(aa); var twoUrl = encodeURI("two.html?ownText=" + JSON.stringify(aaS)); //使用encodeURI編碼 location.href = twoUrl; }) </script> </body> </html>
第二個頁面接收父頁面的地址以及里面的值,再對其進行轉碼 , 輸出就可以了
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.js"></script> </head> <body> <input type="text" class="input"/> <script type="text/javascript"> //獲取 上一個搜索頁面傳來的參數 var twoText = window.location.href; var twodata = twoText.split("="); //截取 url中的“=”,獲得“=”后面的參數 var twoText = decodeURI(twodata[1]); //decodeURI解碼 $(".input").val(twoText); </script> </body> </html>