简体   繁体   English

如何根据单击的按钮更改跨度文本?

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

I want to change the amount shown on the html page after user clicks on them.我想在用户点击后更改 html 页面上显示的金额。 There are 2 buttons and upon clicking each button, it should direct them to a new html and the amount should change accordingly.有 2 个按钮,单击每个按钮后,应将它们定向到新的 html,数量应相应更改。 I know this isn't really the method to do it but I am unsure how else can I do it.我知道这不是真正的方法,但我不确定我还能怎么做。 Currently, I am only able to append all the amount.目前,我只能到 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>");
    }
})();

Want to achieve the results of: eg by clicking the first button, redirects user to pay.html and show $100.想要达到的结果:例如通过点击第一个按钮,将用户重定向到pay.html 并显示$100。 By clicking the second button, redirects to the same html(pay.html), show $300.通过单击第二个按钮,重定向到相同的 html(pay.html),显示 $300。 Please help thanks a lot.请帮助非常感谢。

you can give id to span and then set the text to 300 or 100您可以将 id 设置为 span,然后将文本设置为 300 或 100

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

Send GET request to pay.html with params:pay.html发送GET请求,参数如下:

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

Access params through JS:通过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 : 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';
};

pay.html :支付.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>");

This will also work without Real app.这也将在没有 Real 应用程序的情况下工作。

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

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