gpt4 book ai didi

javascript - 单击phonegap中的href链接时如何通过url传递参数?

转载 作者:行者123 更新时间:2023-12-03 11:24:20 24 4
gpt4 key购买 nike

您好,我正在尝试在 PhoneGap 中的两个页面之间传递参数

$('.left_menu').append('<ul>'+
'<li><a href="A.html?username="+employeecode+"&role="+rolecode+"" rel="external">A</a></li>'+
'<li><a href="B.html?username="+employeecode+"&role="+rolecode+"" rel="external">B</a></li>'+
'<li><a href="C.html?username="+employeecode+"&role="+rolecode+"" rel="external">C</a></li>'+
'<li><a href="D.html?username="+employeecode+"&role="+rolecode+"" rel="external">D</a></li>'+
'</ul>');

A.html页面中获取参数我使用了以下代码:

function GetUrlValue(VarSearch){
var SearchString = window.location.search.substring(1);
var VariableArray = SearchString.split('&');
for(var i = 0; i < VariableArray.length; i++){
var KeyValuePair = VariableArray[i].split('=');
if(KeyValuePair[0] == VarSearch){
return KeyValuePair[1];
}//END IF
}//END FOR
}//END FUNCTION

var employeecode = decodeURIComponent(GetUrlValue('username'));
var rolecode = decodeURIComponent(GetUrlValue('role'));

最佳答案

您可以添加以下代码来设置 url 并获取 url 参数值。

$('.left_menu').append('<ul>'+
'<li><a href="A.html?username="'+employeecode+'&role='+rolecode+'" rel="external">A</a></li>'+
'<li><a href="B.html?username="'+employeecode+'&role='+rolecode+'" rel="external">B</a></li>'+
'<li><a href="C.html?username="'+employeecode+'&role='+rolecode+'" rel="external">C</a></li>'+
'<li><a href="D.html?username="'+employeecode+'&role='+rolecode+'" rel="external">D</a></li>'+
'</ul>');

在A.html页面

/** GET PARAMETER **/
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return results[1];
}

var username = getParameterByName('username');
var role = getParameterByName('role');

关于javascript - 单击phonegap中的href链接时如何通过url传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26986752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com