简体   繁体   English

如何使用 JS 为基于键的输入赋值?

[英]How can I use JS to assign a value on an input based on a key?

Okay, take two.好吧,拿两个。 Before you continue reading please note that I know basically ZERO JS so if I display some extreme stupidity, I'm trying my best.在你继续阅读之前,请注意我基本上知道零 JS,所以如果我表现出一些极端的愚蠢,我会尽力而为。

I am attempting to implement PayPal into a website I'm creating and I found a button on the PayPal website that was basically exactly what I'm looking for.我正在尝试将 ZAD69E733EBCAA8D264BCCAA38D68830E8Z 实施到我正在创建的网站中,并且我在 PayPal 网站上找到了一个按钮,该按钮基本上正是我正在寻找的。

I already have a working cart price calculation system that I've had in place for a while now and calculates Sales Tax/Shipping/The total price of the items in the cart TotalPrice and puts it into one h1 tag using document.getElementByID('ID').innerHTML = `$${localStorage.getItem('TotalPrice')}` This works perfectly well, and has for a few months.我已经有一个工作车价格计算系统,我已经有一段时间了,它计算销售税/运费/购物车TotalPrice中物品的总价格,并使用document.getElementByID('ID').innerHTML = `$${localStorage.getItem('TotalPrice')}`这工作得非常好,并且已经使用了几个月。

The button code exactly from the PayPal website is as follows: 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>

and the value that actually sets the price PayPal charges once the form is submitted is in the hidden input with the name "amount".提交表单后,实际设置 PayPal 收取的价格的值位于名为“金额”的隐藏输入中。 I figured that it would be relatively easy to set the value of that input by adding an ID and using similar logic to the working cart system.我认为通过添加一个 ID 并使用与工作车系统类似的逻辑来设置该输入的值会相对容易。 Here is what I have now:这是我现在拥有的:

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

I also added some JS我还添加了一些 JS

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

What I tried to do was, when the button is clicked and the form is submitted, change the value of the form so that it matches the total value of the items in the cart, TotalPrice .我尝试做的是,当单击按钮并提交表单时,更改表单的值,使其与购物车中物品的TotalPrice相匹配。 What have I done wrong?我做错了什么?

If you need more information, I will do my best to provide it through edits & comments.如果您需要更多信息,我将尽我所能通过编辑和评论提供。 Thanks.谢谢。

You're better off using a JS smart button ( code demo ).你最好使用JS 智能按钮代码演示)。

Then in createOrder you can just replace the price with localStorage.getItem('TotalPrice')然后在createOrder中,您可以将价格替换为localStorage.getItem('TotalPrice')

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

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