簡體   English   中英

如何根據單擊的按鈕更改跨度文本?

[英]How can I change span text based on the button clicked?

我想在用戶點擊后更改 html 頁面上顯示的金額。 有 2 個按鈕,單擊每個按鈕后,應將它們定向到新的 html,數量應相應更改。 我知道這不是真正的方法,但我不確定我還能怎么做。 目前,我只能到 append 的所有金額。

<button onclick="myFunction1()" type="submit" id="one">BUY</button>
<button onclick="myFunction2()" type="submit" id="two">BUY</button>
function myFunction1() {
    window.location='pay.html';
};

function myFunction2() {
    window.location = 'pay.html';
};
 <span class="title" id="total"></span>
 <span class="title" id="total2"></span>
(function () {
    "use strict";

    $().ready(function () {

    })
    myFunction1()

    function myFunction1() {
        var totalamt = '$100';
        $('#total').html("<span style='float:right'>" + "Total:" + 
        totalamt + "</span>");
    }

})();

(function () {
    "use strict";

    $().ready(function () {

    })
    myFunction2()

    function myFunction2() {
        var totalamt2 = '$300';
        $('#total2').html("<span>" + "Total:" + totalamt2 + 
        "</span>");
    }
})();

想要達到的結果:例如通過點擊第一個按鈕,將用戶重定向到pay.html 並顯示$100。 通過單擊第二個按鈕,重定向到相同的 html(pay.html),顯示 $300。 請幫助非常感謝。

您可以將 id 設置為 span,然后將文本設置為 300 或 100

 <span id="price">$100</span>
   <scrit>
   document.getElementById("price").innerHTML = "$300";  
</script>

pay.html發送GET請求,參數如下:

window.location='pay.html?amount=100';

通過JS訪問參數:

var url= new URL(document.location).searchParams;
var amount=url.get("amount");
var totalamt = '$'+ amount;
$('#total').html("<span style='float:right'>" + "Total:" + totalamt + "</span>");

yourhtml.html

<button onclick="myFunction1()" type="submit" id="one">BUY</button>
<button onclick="myFunction2()" type="submit" id="two">BUY</button>
function myFunction1() {
    window.location='pay.html?amount=100';
};

function myFunction2() {
    window.location = 'pay.html?amount=300';
};

支付.html

 <span class="title" id="total"></span>
var url = new URL(document.location).searchParams;//Getting search params
var amount = url.get("amount"); //Getting value of amount from parameters
var totalamt = '$'+ amount;
$('#total').html("<span style='float:right'>" + "Total:" + totalamt + "</span>");

這也將在沒有 Real 應用程序的情況下工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM