这看起来很熟悉:
How to redirect on another page and pass parameter in url from table?
<a href="#" id="redirectwithid">link</a>
原生 JS:
document.getElementById('redirectwithid').setAttribute('href','yoururl?variable=' + variableGetter());
jQuery:
$("#redirectwithid").click(function() {
var yourVariable = variableGetter();
if (yourVariable != null) {
window.location = 'yoururl?variable=' + yourVariable;
}
});
为原生 JS 添加:
我不知道你的代码,所以只需在你想要更新变量的地方插入代码 - 同时 href 会改变。
编辑:
如果您想添加多个变量,例如 ?variable=1 和第二个 variable2=2,您可以通过在变量之间添加 & 来实现,如下所示:
NativeJS:
document.getElementById('redirectwithid').setAttribute('href','yoururl?variable=' + variableGetter() + '&variable2=' + variable2Getter());
JQuery:
$("#redirectwithid").click(function() {
var yourVariable = variableGetter();
var yourVariableTwo = variableTwoGetter();
if (yourVariable != null) {
window.location = 'yoururl?variable=' + yourVariable + '&' + yourVariableTwo;
}
});