简体   繁体   中英

On Select Change href and Text

I am trying to change price and href when user select from drop down menu which is working fine.

Now I want to change a text somewhere in the page.

Change Text required here

<div class="price">
    <strong id="OrderLinkOne">2.00</strong>
    <span>per month</span>
</div>

I am also using a PHP Function to make it more dynamic below is the function

$product_id1        =   5;
$product_id2        =   6;
$product_id3        =   7;
$product_id4        =   8;
$tenure             =   12;

function SelectValue($price, $pid, $billingcycle){
    $link ="http://project.dev/cart.php?a=add&pid=".$pid."&billingcycle="; // LIVE

    if( $billingcycle == "1"  ){
        echo '<option value="'.$link.'monthly">1 Month at $'.$price.'/month</option>';
    }

    if( $billingcycle == "3"  ){
        echo '<option value="'.$link.'quarterly">3 Months at $'.$price.'/month</option>';
    }

    if( $billingcycle == "6"  ){
        echo '<option value="'.$link.'semiannually">6 Months at $'.$price.'/month</option>';
    }

    if( $billingcycle == "12"  ){
        echo '<option value="'.$link.'annually">12 Months at $'.$price.'/month</option>';
    }

    if( $billingcycle == "24"  ){
        echo '<option value="'.$link.'biennially">24 Months at $'.$price.'/month</option>';
    }

    if( $billingcycle == "36"  ){
        echo '<option selected="selected" value="'.$link.'triennially">36 Months at $'.$price.'/month</option>';
    }

}

function SelectPrice($HTMLID, $pid, $billingcycle){
    $link ="http://project.dev/cart.php?a=add&pid=".$pid."&billingcycle="; // LIVE

    if( $billingcycle == "1"  ){
        echo '<a id="'.$HTMLID.'" href="'.$link.'monthly">Buy Now</a>';
    }

    if( $billingcycle == "3"  ){
        echo '<a id="'.$HTMLID.'" href="'.$link.'quarterly">Buy Now</a>';
    }

    if( $billingcycle == "6"  ){
        echo '<a id="'.$HTMLID.'" href="'.$link.'semiannually">Buy Now</a>';
    }

    if( $billingcycle == "12"  ){
        echo '<a id="'.$HTMLID.'" href="'.$link.'annually">Buy Now</a>';
    }

    if( $billingcycle == "24"  ){
        echo '<a id="'.$HTMLID.'" href="'.$link.'biennially">Buy Now</a>';
    }

    if( $billingcycle == "36"  ){
        echo '<a id="'.$HTMLID.'" href="'.$link.'triennially">Buy Now</a>';
    }
}

My JS:

$(document).ready(function(){
  $("#planOne").change(function () {
    console.log(this.value);
    $("#OrderLinkOne").attr('href', this.value);
  });
});

HTML

<div class="select-tenure">
    <select id="planOne">
        <?php echo SelectValue("2.08", $product_id1, 12);?>
        <?php echo SelectValue("2.00", $product_id1, 24);?>
        <?php echo SelectValue("2.00", $product_id1, 36);?>
    </select>
</div>

<div class="link">
    <?php echo SelectPrice("OrderLinkOne", $product_id1, 36);?>
</div>

When I change from dropdown the link is wokring fine but I want to change only price under .price class so please provide a solution how to show only price instead of complete value of an option...

This is what you need.

$(".price").children("strong").text("3.00"); //change the value 2.00

$("span").text("per week"); // change the text within the span

You can use if or switch to change the value based on the user choice.

 $(document).ready(function(){ $("#planOne").change(function () { console.log(this.value); $("#OrderLinkOne").attr('href', this.value); $(".price").children("strong").text("3.00"); $("span").text("per week"); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="price"> <strong>2.00</strong> <span>per month</span> </div> <select id="planOne"> <option value="google.com">Google</option> <option value="microsoft.com">Microsoft</option> <option value="yahoo.com">Yahoo</option> </select> <a id="OrderLinkOne" href="google.com">Link</a>

Okay for a simple solution you can write the code like:

$(document).ready(function(){
  $("#planOne").change(function () {
    console.log(this.value);
    $("#OrderLinkOne").attr('href', this.value);
    $("#PriceOne").html('1000'); // you can turn this interger in what you want
  });
});

now you can change your price in every integer you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM