简体   繁体   English

在选择更改 href 和文本

[英]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.当用户从工作正常的下拉菜单中选择时,我试图更改价格和 href。

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我还使用了一个 PHP 函数来使它更加动态,下面是函数

$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:我的JS:

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

HTML 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...当我从下拉列表中更改时,链接工作正常,但我只想更改.price类下的价格,因此请提供一个解决方案,如何仅显示价格而不是选项的完整价值...

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.您可以使用ifswitch根据用户选择更改值。

 $(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.现在您可以更改您想要的每个整数的价格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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