How to change a <a> link according to a <select>?

Asked

Viewed 801 times

4

I’m creating a price list for my website, and I need when the person changes the payment cycle, the link be changed <a> (HIRE) to go to the cart.

Example: If the person selects the cycle Monthly, the link changes to:

http://meusite.com/carrinho.php?produto=1&periodo=mensal

If the person chooses Quarterly, the link changes to:

http://meusite.com/carrinho.php?produto=1&periodo=trimestral

If the person chooses Half-yearly, the link changes to:

http://meusite.com/carrinho.php?produto=1&periodo=semestral

And if the person chooses Yearly, the link changes to:

http://meusite.com/carrinho.php?produto=1&periodo=anual

EXAMPLE IN Jsfiddle

3 answers

4

Dude I made some changes to your Fiddle to run a look there. I found it a little confusing because you were already using the value of combo options to control value writing, so I had to create an attribute in the options called "data-opt" with the values {M,T,S,A}. Another change I had to make was to put id in the links and in each select I created an attribute data-link to match each combo with its link.

It was not the most correct way to make this working more.

  • It was very good, however, I forgot to mention one thing... Each table of this is a different product, IE, 3 different tables. Then you have to make a change in this jQuery. Ex: http://meusite.com/carrinho.php?produto=1&periodo=mensal and http://meusite.com/carrinho.php?produto=2&periodo=mensal and http://meusite.com/carrinho.php?produto=3&periodo=mensal

  • I changed the Fiddle link there in the answer, I created another data-product attribute in the combo, so for each product you have to feed this attribute.

  • Check it out: http://jsfiddle.net/AlexandreBR/23met8eu/5/ I made some changes to the HTML you forgot. They were all like produto1

  • It seems that was perfect, I will do some tests, if all goes well, I come here and give as best answer. : D Thanks!

2

You can do it like this:

$(".select-ciclos").heapbox({
'onChange':function(value,src){

    var mostrador = $(src).closest('.pricing-column').find('.trans');
    var val = value;
    mostrador.animate({
        opacity: 0
    }, 250, function () {
        if (val == "<sup>R$</sup><span>6,99</span>/mês")
        {
        $("#cont").attr("href", "http://meusite.com/carrinho.php?produto=1&periodo=mensal");
        }
        else if (val == "<sup>R$</sup><span>28,77</span>")
        {
        $("#cont").attr("href", "http://meusite.com/carrinho.php?periodo=trimestral");
        }
        else if (val == "<sup>R$</sup><span>54,14</span>")
        {
        $("#cont").attr("href", "http://meusite.com/carrinho.php?produto=1&periodo=semestral");
        }
        else if (val == "<sup>R$</sup><span>99,99</span>/ano")
        {
        $("#cont").attr("href", "http://meusite.com/carrinho.php?produto=1&periodo=anual");
        }
        mostrador.html(val);
        mostrador.animate({
            opacity: 1
        }, 250)
    })
    }
});


$(document).ready(function() {
   $(".select-ciclos").heapbox();
 });

You also have to change the html of the "HIRE" link by adding an id:

<a href="http://meusite.com/carrinho.php?periodo=mensal" id="cont">

Jsfiddle

  • Each table of this is a different product, ie 3 different tables. So you have to make a change in this jQuery. Ex: http://meusite.com/carrinho.php?produto=1&periodo=mensal and http://meusite.com/carrinho.php?produto=2&periodo=mensal and http://meusite.com/carrinho.php?produto=3&periodo=mensal

2


I think they have complicated too much, select should provide you with the value you really need, which is the periodicity, and the price is that it should come as an attribute date, and only the value without formatting...

I recommend something like this:

HTML: (only the modifications)

Change the attribute value for the periodicity and create the attribute data-preco with the price:

                <select class="select-ciclos" name="select">
                    <option value="mensal" selected="selected" data-preco="6,99">Mensal</option>
                    <option value="trimestral" data-preco="28,77">Trimestral -4%</option>
                    <option value="semestral" data-preco="54,14">Semestral -8%</option>
                    <option value="anual" data-preco="99,99">Anual -16%</option>
                </select>

Jquery:

$(".select-ciclos").heapbox({
    'onChange': function (value, src) {
        $(src).closest('.pricing-column').find('.trans').animate({
            opacity: 0
        }, 250, function () {
            $(this).children('span').html($(src).find("option:selected").attr("data-preco"));
            if (value == 'mensal') var ciclo = '/mês';
            if (value == 'anual') var ciclo = '/ano';
            $(this).html( $(this).html().replace(/<\/span>.*/g, '</span>' + (ciclo ? ciclo : '')) );
            $(this).animate({
                opacity: 1
            }, 250)
        })
        var elementolink = $(src).closest('.pricing-column').find('.pricing-footer a');
        var link = elementolink.attr("href");
        elementolink.attr("href", link.replace(/periodo\=(.+?)(&|$)/g, 'periodo=' + value + '&').replace(/&$/g, ''));
    }
});

See working on Jsfiddle

  • Your Fiddle has 2 problems: in the first table does not change the link URL (I already found: http://jsfiddle.net/z04dp5ds/1/). I used the code in value precisely because of this, because when you change the payment cycle continues the name "/month" next to the price (blank).

  • Example - code I was using: <option value="<sup>R$</sup><span>6,99</span>/mês"

  • Your code was exceptional, however, the only problem is this "/month, /year, etc."

  • @Alexandrelopes On the first link I purposely modified the order of the parameters (of: carrinho.php?produto=1&periodo=mensal for carrinho.php?periodo=mensal&produto=1) to test the substitution regex, and I just tested it on IE, FF and CH and they all do the substitution correctly, are you sure it’s not changing? Which browser do you use? As for the /month and /year really hadn’t noticed, I just edited the answer with a possible solution, jsfiddle was also edited.

  • Now the 1° table is working normal :D, is because you forgot to put produto= on your first fiddle. Only now you have a little problem @Jader the names "/month, /year, etc." should stay after </span> (6,99</span>/month) ex: <div class="trans"><sup>R$</sup><span>6,99</span>/mês</div>. In your example everything is together (6.99/month): <div class="trans"><sup>R$</sup><span>6,99/mês</span></div>

  • Here’s what it looks like when it’s stylized: http://i.imgur.com/4I4KDIw.png | @Jader data-ciclo="" and within it the value "/month, /year, etc.". You could make these names "/month, /year, etc." appear after the </span> or create a <span> to put the name inside. Thank you Jader! ;)

  • @Alexandrelopes I removed the /month of the price date and used if value == mensal/anual to replace the end of html, without having to create another date attribute...

  • But it will not get good! Rsrs, has to be the month name and not monthly. Thank you so much for the strength!

  • @Alexandrelopes analyze the code well before commenting... And watch it work in Jsfiddle...

  • Oops! I’m sorry! rsrsr I just looked at HTML. lol

  • LOL, got very crazy bitchu! Vlw man, deserves better answer! D

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.