javascript使函数参数动态化

时间:2019-06-03 05:50:20

标签: javascript html

所以我试图在我的order.html页面上包括付款API结帐,但是,您必须在HTML文档的开头添加脚本。在脚本中,您必须将小计金额指定为数字,但是我需要此值是动态的。 “总计”是小计金额。我需要将小计设为“总计”。我该怎么做?

<head>
  <script type="text/javascript">
    function paymentApi() {
      V.init({
        apikey: "",
        encryptionKey: "",
        paymentRequest: {
          currencyCode: "USD",
          subtotal: "11.00"
        }
      });
  </script>
</head>

<body>
  <p id="sub-total">
    <strong>Total</strong>: <span id="stotal"></span>
  </p>

2 个答案:

答案 0 :(得分:1)

只需更改小计即可从html中获取值,如下所示

<script type="text/javascript">
function paymentApi(){
    V.init( {
    apikey: "",
    encryptionKey: "",
    paymentRequest:{
        currencyCode: "USD",
        subtotal: document.getElementById('stotal').innerText //Here
    }
    });
</script>

答案 1 :(得分:-4)

<script type="text/javascript">
function paymentApi(){
    var subtotal = parseFloat($("#stotal").text());

    V.init( {
    apikey: "",
    encryptionKey: "",
    paymentRequest:{
        currencyCode: "USD",
        subtotal: subtotal
    }
    });

</script>