2022-03-14 工作记录--JS-window.location.href跳转页面时传递参数并且在新页面接收参数

举个例子:假如从A页面跳转到B页面,并携带参数convenient;然后在B页面那里接收到该参数。

A页面

function transformPage() {
    
    
    var convenient = $('select[name="convenient"]').val();
    window.location.href = "{:url('finances/listtoexamine')}?convenient="+convenient; // 跳转页面并携带参数convenient
}

B页面

var get_url = location.search; //获取url中"?"符及其后面的字符串 ('?convenient=1')
var theRequest = new Object(); // 自定义一个空对象 {}
if ( get_url.indexOf( "?" ) != -1 ) {
    
     // 如果get_url里面包含了"?"符
    var str = get_url.substr(1); //substr()方法返回从参数值开始到结束的字符串,即去掉"?"符;
    var strs = str.split("&");
    for ( var i = 0; i < strs.length; i++ ) {
    
    
        theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
    }
}
console.log( theRequest ); //此时的theRequest就是我们需要的参数的对象合集 {convenient: '1'}

if(theRequest.convenient && theRequest.convenient != '') {
    
    
   $('select[name="convenient"]').val(theRequest.convenient);
}

参考博文:https://blog.csdn.net/qq_29072049/article/details/80221694

猜你喜欢

转载自blog.csdn.net/weixin_48850734/article/details/123531980