欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js/jquery 获取url传递参数,js获取url?号后面的参数(一个或多个)

程序员文章站 2024-02-17 21:53:58
...

第一种方法:
(获取一个)
detail.html?order_id=10

<Script language="javascript">  
    function GetRequest() {  
       var url = location.search; //获取url中"?"符后的字串  
       var theRequest = new Object();  
       if (url.indexOf("?") != -1) {  
          var str = url.substr(1);  
          strs = str.split("&");  
          for(var i = 0; i < strs.length; i ++) {  
             theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
          }  
       }  
       return theRequest;  
    }  
    </script> 

调取

$(document).ready(function(){
    var a=GetRequest();
    var order_id = a['order_id'];
    // console.log(order_id)
	Detail(order_id);
     })

获取两个的(仔细看差别)
detail.html?order_id=10?uid=3

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
     if (url.indexOf("?") != -1) {
           var str = url.substr(1);
           strs = str.split("?");
           console.log(strs)
           for (var i = 0; i < strs.length; i++) {
               theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
           }
       }
       return theRequest;
    }

调用

$(document).ready(function(){
    var a=GetRequest();
    var order_id = a['order_id'];
    var uid = a['uid'];  
    // console.log(order_id)
    //console.log(uid)
    Detail(order_id,uid);
 })

其他方法参见下面这篇博客
https://www.cnblogs.com/karila/p/5991340.html

相关标签: url 获取参数