简体   繁体   English

使用 JavaScript 提交表单时,如何从用户那里获取数量变量?

[英]How can I get quantity variable from user when subnmitting a form using JavaScript?

It's a PayPal Subscription Plan and unlike their old subscription plans that were HTML forms, these are JavaScript only.这是一个 PayPal 订阅计划,与他们旧的 HTML 表单订阅计划不同,它们只是 JavaScript。

The plan is set up with different pricing depending on quantity ordered, but PayPal doesn't give you any way of getting the user to submit quantity and just defaults to a quantity of 1. I can manually set the quantity in the script (shown below with quantity of 4) which works just fine but there's no point in having this sort of button if I need a different button for each quantity.该计划根据订购的数量设置了不同的定价,但 PayPal 没有给您任何让用户提交数量的方法,只是默认数量为 1。我可以在脚本中手动设置数量(如下所示)数量为 4) 效果很好,但是如果我需要为每个数量使用不同的按钮,那么使用这种按钮是没有意义的。

Here is the PayPal code with identifiers removed;这是删除了标识符的 PayPal 代码;

<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=ABCD-ABC_1234ABCD&vault=true&intent=subscription" data-sdk-integration-source="button-factory"></script>
<script>
  paypal.Buttons({
      style: {
          shape: 'rect',
          color: 'gold',
          layout: 'vertical',
          label: 'subscribe'
      },
      createSubscription: function(data, actions) {
        return actions.subscription.create({
          'plan_id': 'P-123456789'
          'quantity': '4'
        });
      },
      onApprove: function(data, actions) {
        alert(data.subscriptionID);
      }
  }).render('#paypal-button-container');
</script>

So what I need is a way to have the actual quantity dynamically updated by the user before they hit the Subscribe button.所以我需要的是一种让用户在点击订阅按钮之前动态更新实际数量的方法。 I assume this is possible via JS but I have no idea how.我认为这可以通过 JS 实现,但我不知道如何实现。 The same question has been asked twice on the PayPal Community Website but no answers have yet been posted.同样的问题在 PayPal 社区网站上已被问过两次,但尚未发布任何答案。

Create an HTML input or select dropdown that allows selecting the quantity.创建允许选择数量的 HTML 输入或选择下拉列表。 Simple example:简单的例子:

<span>Qty: </span><input id="myQuantity" type="text" />

.... ....

      createSubscription: function(data, actions) {
        return actions.subscription.create({
          'plan_id': 'P-123456789'
          'quantity': document.getElementById('myQuantity').value
        });
      },

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

相关问题 我如何使用flask从javascript获取变量 - how can I get variable from javascript using flask 如何在没有用户单击提交的情况下从表单设置JavaScript变量? - How can I set a JavaScript variable from a form without a user clicking submit? 使用 Javascript,如何检测按键事件而不是用户在表单字段中键入时? - Using Javascript, how can I detect a keypress event but NOT when the user is typing in a form field? 我如何获取用户数据并将 output 放入表单 | javascript - How can i get user data and put the output in the form | javascript 如何从一个表单获取变量到javascript弹出窗口? - How can I get a variable from one form into a javascript popup window? 如何从HTML表单获取用户输入并将其存储在现有的JavaScript数组中 - How can I get the user input from a HTML form and store it in an existing JavaScript array 如何使用此javascript函数将变量发送到表单? - How can I send a variable to a form using this javascript function? 使用托管字段时,如何检测用户何时使用 Braintree JavaScript SDK 提交表单? - How can i detect when user submits the form when using Braintree JavaScript SDK when using hosted fields? 如何限制用户输入的数量? - How can I limit the user input in quantity? 如何使用JavaScript在PHP GET变量上运行if语句? - How can I run an if statement on a PHP GET variable using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM