开发者

Change part of an anchor link attribute with JavaScript

开发者 https://www.devze.com 2023-03-21 20:41 出处:网络
I\'m trying to change the middle part of the URL when the proper radio button is clicked. In the three purchase links each one represents an amount. I\'m going to be selecting an animal then clicking

I'm trying to change the middle part of the URL when the proper radio button is clicked.

In the three purchase links each one represents an amount. I'm going to be selecting an animal then clicking on 1 of 3 amounts I want to spend. How would I change part of all 3 of the URLs?

How could I go 开发者_高级运维about doing that? These are the radio buttons (with JavaScript):

<input type="radio" name="orderID" 
    value="-DOL&linkID=naib&url=https%3A//example.com/List%3FlinkID%3Dnaib"
    onclick="document.getElementById('editBTN').href=this.value">
    Dolphin
</input>
<input type="radio" name="orderID" 
    value="-SHK&linkID=naib&url=https%3A//example.com/List%3FlinkID%3Dnaib"
    onclick="document.getElementById('editBTN').href=""+this.value">
    Shark
</input>

And the purchase links (250$, 100$ and 50$, respectively):

<a id="editBTN" href="http://example.com/Info?ticketCode=%XX%3A%Q250">Purchase</a>
<a id="editBTN" href="http://example.com/Info?ticketCode=%XX%3A%Q100">Purchase</a>
<a id="editBTN" href="http://example.com/Info?ticketCode=%XX%3A%Q50">Purchase</a>

The DOL part is the only part that needs to change.


If you're going to rename the Id's on your buttons to be unique, make a function:

<script type="text/javascript">
    function setAnimal(obj) {
        var baseURL="http://ev8.evenue.net/cgi-bin/ncommerce3/SEGetEventInfo?ticketCode=%3ANAIB%3A%3AAQ";
        document.getElementById('editBTN1').href=baseURL+"250&"+obj.value;
        document.getElementById('editBTN2').href=baseURL+"100&"+obj.value;
        document.getElementById('editBTN3').href=baseURL+"50&"+obj.value;
    }
</script>

and then, on the radio buttons do:

onclick="setAnimal(this);"


onclick="document.getElementById('editBTN').href='http://ev8.evenue.net/cgi-bin/ncommerce3/SEGetEventInfo?ticketCode=%3ANAIB%3A%3AAQ250'+this.value;"


There is no middle part, use regular expressions, to cut the string and use the one you want. Changing it is quite easy:

var edBTN = getElementById('editBTN');
edBTN.setAttribute('href', mynewurl);


with jquery or another dom parsing library this should be possible.

if ( /* radio selected */ ) {

    // Go get your src attrib
    var src = jQuery( 'selectorforanchortag' ).attr('src');
    // Manipulate the src however you need using javascript string methods
    src = src.blergetyblerg();
    // Put your new src back where orig was
    jQuery( 'selectorforanchortag' ).attr('src');

}


onclick="javascript:document.getElementById('editBTN').href=this.value">
0

精彩评论

暂无评论...
验证码 换一张
取 消