我目前在其网址为http://localhost:49852/Default.aspx?MID=17
在此页面中我有以下代码
<a href="Configure.aspx"><span class="ico gray shadow gear"></span>Configure</a>
我希望一旦用户点击Configure
链接,就会裁剪来自网址的MID参数,用户将被重定向到
http://localhost:49852/Configure.aspx?MID=17
这是JS代码
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)')
.exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
如何将它们全部绑定在一起?
答案 0 :(得分:3)
首先,我认为你应该给你的href一个id,方便访问,如:
<a href="Configure.aspx" id="configure"><span class="ico gray shadow gear"></span>Configure</a>
然后你应该可以用:
更改属性$(function() {
var href = $("#configure").attr("href");
$("#configure").attr("href", href + getParameterByName("MID"))
});
这将改变实际的href。您可以轻松地覆盖click方法,但如果您想将其从configure链接更改为所有链接或其他内容,则可能更具可扩展性。
答案 1 :(得分:0)
您可以在元素上附加单击事件侦听器,单击此按钮时,您可以修改window.location
以执行重定向。像这样:
$(function () {
// You might need to use a better selector here,
// add an ID to the span element and select on that instead
$("span.ico").on("click", function () {
window.location.href = "Configure.aspx?MID=" + getParameterByName("MID");
return false;
});
});
答案 2 :(得分:0)
您可以使用这个不错的函数从QueryString解析MID 正如Parse query string in JavaScript所述 用它构建一个新的URL。
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
console.log('Query variable %s not found', variable);
}
喜欢;
var mid = getQueryVariable(MID);
var url = 'http://localhost:49852/Configure.aspx?MID='+mid;
location.href=url;