【问题标题】:How can I use JS to assign a value on an input based on a key?如何使用 JS 为基于键的输入赋值?
【发布时间】:2022-01-11 21:40:22
【问题描述】:

好的,拿两个。 在你继续阅读之前,请注意我基本上知道零 JS,所以如果我表现出一些极端的愚蠢,我会尽力而为。

我正在尝试将 PayPal 应用到我正在创建的网站中,我在 PayPal 网站上找到了 button,这基本上正是我正在寻找的。​​p>

我已经有一个工作车价格计算系统,我已经有一段时间了,它计算销售税/运费/购物车中物品的总价格 TotalPrice 并将其放入一个 h1 标签中document.getElementByID('ID').innerHTML = `$${localStorage.getItem('TotalPrice')}` 这个效果很好,已经用了几个月了。

来自PayPal网站的按钮代码如下:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<!-- Identify your business so that you can collect the payments. --> <input type="hidden" name="business" value="herschelgomez@xyzzyu.com">
 <!-- Specify a Buy Now button. --> 
<input type="hidden" name="cmd" value="_xclick"> 
<!-- Specify details about the item that buyers will purchase. --> 
<input type="hidden" name="item_name" value="Hot Sauce-12oz. Bottle"> 
<input type="hidden" name="amount" value="5.95"> 
<input type="hidden" name="currency_code" value="USD"> 
<!-- Display the payment button. --> 
<input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="Buy Now"> 
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" > </form>

一旦提交表单,实际设置 PayPal 收取的价格的值位于名为“金额”的隐藏输入中。我认为通过添加一个 ID 并使用与工作车系统类似的逻辑来设置该输入的值会相对容易。这是我现在拥有的:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
                <!-- Identify your business so that you can collect the payments. --> 
                <input type="hidden" name="business" value="website@gmail.com"> 
                <!-- Specify a Buy Now button. --> 
                <input type="hidden" name="cmd" value="_xclick"> 
                <!-- Specify details about the item that buyers will purchase. --> 
                <input type="hidden" name="item_name" value="Checkout"> 
                <input type="hidden" name="amount" id="pricebutton" > 
                <input type="hidden" name="currency_code" value="USD"> 
                <!-- Display the payment button. --> 
                <input type="submit" name="submit" border="0" id="checkoutbtn" alt="Buy Now" value="Checkout Using Paypal" onclick="payPal()">
            </form>

我还加了一些JS

<script>
function payPal() {
    document.getElementById('pricebutton').value = localStorage.getItem('TotalPrice');
}
</script> 

我尝试做的是,当单击按钮并提交表单时,更改表单的值,使其与购物车中物品的总值相匹配,TotalPrice。我做错了什么?

如果您需要更多信息,我会尽力通过编辑和 cmets 提供。 谢谢。

【问题讨论】:

    标签: javascript html paypal


    【解决方案1】:

    你最好使用a JS smart button (code demo)。

    然后在createOrder 中,您可以将价格替换为localStorage.getItem('TotalPrice')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2013-04-27
      • 2020-11-07
      相关资源
      最近更新 更多