javascript javascript将变量添加到链接的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3619847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript add variable onto end of link
提问by Jamie Taylor
I'm trying to add a variable i created on to the end of one of my links but not sure how to do it?
我正在尝试将我创建的变量添加到我的一个链接的末尾,但不知道该怎么做?
<a href="../../availability/default.aspx?propid=' + myvariable + '">Link</a>
Any ideas?
有任何想法吗?
Thanks
谢谢
Jamie
杰米
采纳答案by palswim
Something like this will create a closure which will store your original HREFproperty:
像这样的事情会创建一个闭包来存储你的原始HREF属性:
function init() {
var link = document.getElementById("link");
var hrefOrig = link.href;
var dd = document.getElementById("DropDown");
dd.onchange = function(){ link.href = hrefOrig + dd.value; }
}
window.addEventListener("load", init, false); // for Firefox; for IE, try window.attachEvent
回答by Adam
Add an ID:
添加标识:
<a id="link" href="../../availability/default.aspx?propid=">Link</a>
JavaScript:
JavaScript:
document.links["link"].href += myvariable;
jQuery:
jQuery:
$('#link').attr('href', $('#link').attr('href') + myvariable);
回答by Luka Milani
The solution is only to adapt the code that Adam post above so:
解决方案只是调整 Adam 在上面发布的代码,以便:
HTML
HTML
<a id="link" href="">Link</a>
<select onchange="addVariable(this.value)">...
Javascript
Javascript
function addVariable(myvariable){
document.links["link"].href = "../../availability/default.aspx?propid=" + myvariable;
}

