繁体   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