最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript add variable onto end of link - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Sep 1, 2010 at 15:47 Jamie TaylorJamie Taylor 3,53022 gold badges69 silver badges100 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Add an ID:

<a id="link" href="../../availability/default.aspx?propid=">Link</a>

JavaScript:

document.links["link"].href += myvariable;

jQuery:

$('#link').attr('href', $('#link').attr('href') + myvariable);

Something like this will create a closure which will store your original HREF property:

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

The solution is only to adapt the code that Adam post above so:

HTML

<a id="link" href="">Link</a>

<select onchange="addVariable(this.value)">...

Javascript

function addVariable(myvariable){

document.links["link"].href = "../../availability/default.aspx?propid=" + myvariable;

}
发布评论

评论列表(0)

  1. 暂无评论