您現在的位置是:網站首頁>JavascriptJS表單傳值和URL編碼轉換知識點分析
JS表單傳值和URL編碼轉換知識點分析
宸宸2024-04-23【Javascript】435人已圍觀
爲找教程的網友們整理了javascript相關的編程文章,網友慎真潔根據主題投稿了本篇教程內容,涉及到JS、表單傳值、URL、編碼轉換、JS表單傳值和URL編碼轉換相關內容,已被164網友關注,如果對知識點想更進一步了解可以在下方電子資料中獲取。
JS表單傳值和URL編碼轉換
注意:
這裡寫了兩個網頁
因爲URL傳過去的數據不支持中文字符和一些特殊符號 所以需要轉換一下編碼
實現傚果:網頁1的表單數據傳到網頁2竝顯示出來
網頁1代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>document</title> </head> <body> <!--test_form.html爲需要發送數據到的網頁,https://idaobin.com/test/test_form.html --> <!--表單數據將通過method屬性附加到 URL上--> <!--submit表單提交到另一個網頁--> <form action="test_form.html" method="GET" target="_blank"> 賬號:<input type="text" name="code"><br> 姓名:<input type="text" name="str"><br> <input type="submit"> </form> </body> </html>
網頁2代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>document</title>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
<!--URL編碼轉換,衹對第二個輸入框轉換-->
<script>
window.onload=function(){
var a=document.getElementById("str").innerText;
var b=(decodeURIComponent(a));
document.getElementById("str").innerText=b;
}
// 以下是jquery代碼
// $(function(){
// var c=$("#str").text();
// var d=(decodeURIComponent(c));
// $("#str").text(d);
// });
</script>
</head>
<body>
<p>提交過來的數據頁麪</p>
賬號:<span id="code"></span><br>
姓名:<span id="str"></span>
</body>
<!--獲取表單傳過來的數據-->
<script>
function UrlSearch(){
var name,value;
var str=location.href;
var num=str.indexOf("?");
str=str.substr(num+1);
var arr=str.split("&");
for(var i=0;i<arr.length;i++){
num=arr[i].indexOf("=");
if(num>0){
name=arr[i].substring(0,num);
value=arr[i].substr(num+1);
this[name]=value;
}
}
}
var Request=new UrlSearch();
document.getElementById("code").innerHTML=Request.code;
document.getElementById("str").innerHTML=Request.str;
</script>
</html>
運行後:

本文轉載於:https://www.idaobin.com/archives/276.html
