使用Stripes支付流程示例时出错

Getting an error when using Stripes payment process example

本文关键字:程示例 出错 Stripes 使用      更新时间:2023-09-26

我使用的是Stripe文档中的代码示例。但是,当处理$form.get(0).submit();时,我会得到这个错误。

Uncaught TypeError: Property 'submit' of object #<HTMLFormElement>
is not a function

我的代码如下:

var stripeResponseHandler = function(status, response) {
  var $form = $('#payment-form');
  if (response.error) {
    // Show the errors on the form
    $form.find('.payment-errors').text(response.error.message);
    $form.find('button').prop('disabled', false);
  } else {
    // token contains id, last4, and card type
    var token = response.id;
    console.log(token);
    // Insert the token into the form so it gets submitted to the server
    $form.append($('<input type="hidden" name="stripeToken" />').val(token));
    // and re-submit
    $form.get(0).submit();    **//Error happens here**
  }
};
jQuery(function($) {
  $('#payment-form').submit(function(e) {
    var $form = $(this);
    // Disable the submit button to prevent repeated clicks
    $form.find('#payment-button').prop('disabled', true);
    Stripe.createToken($form, stripeResponseHandler);
    // Prevent the form from submitting with the default action
    return false;
  });
});

我做错了什么?

在收到错误的行上,请尝试以下操作:

jQuery($form.get(0)).submit();

您的问题是,您得到了第一个HTML元素,但为了对此调用"submit",您需要用jQuery包装它。