4

我提前道歉,因为我对 PHP 相当好,但是当谈到 javascript 和 jquery 时,我真的有点无能为力。我有以下代码,我从https://gist.github.com/boucher/1750375找到的示例编辑,并希望将信用卡 exp 月份和年份字段合并为一个。问题是将它们分开以与创建令牌的条带示例代码一起使用。显然 github 上的示例运行良好,但是当我尝试编辑时,脚本并没有表现出任何错误,但没有像以前一样提交条带化和检索令牌。

$(document).ready(function() {
            $("#payment-form").submit(function(event) {
                // disable the submit button to prevent repeated clicks
                $('.submit-button').attr("disabled", "disabled");
                var expgroup = document.getElementById('date-exp').val;
                var expArray = expgroup.split( '/' );
                var expmm = ( expArray[ 0 ] );
                var expyy = ( expArray[ 1 ] );
                // createToken returns immediately - the supplied callback submits the form if there are no errors
                Stripe.createToken({
                    number: $('.cc').val(),
                    cvc: $('.card-cvc').val(),
                    exp_month: expmm,
                    exp_year: expyy
                }, stripeResponseHandler);
                return false; // submit from callback
            });
        });
4

3 回答 3

4

这行不通。

var expgroup = document.getElementById('date-exp').val;

改用这个:

var expgroup = $("#date-exp").val()

“cc”也是 id 而不是类。你需要使用:

$("#cc").val()

并不是:

$(".cc").val()
于 2013-11-05T16:12:45.953 回答
3
document.getElementById('date-exp').val

Is a mixture of jQuery and DOM idiom. It should either be:

document.getElementById('date-exp').value

or:

$('#date-exp').val()

Also consider checking that / is actually in the value (ie that expArray.length===2).

于 2013-11-05T16:21:02.397 回答
1

Stripe 的 jquery.payment 库已经内置了这个函数:

https://github.com/stripe/jquery.payment#paymentcardexpiryvalstring-and-fnpaymentcardexpiryval

这是一些示例代码。

# form fields on HTML.ERB page
<%= form_tag url do %>
  <%= text_field_tag :expiry, nil, name: nil, placeholder: "MM / YYYY", size: 9, id: "stripe-card-expiry" %>
  <%= hidden_field_tag :exp_month, nil, name: nil, id: "stripe-card-exp-month", data: { stripe: "exp-month" } %>
  <%= hidden_field_tag :exp_year, nil, name: nil, id: "stripe-card-exp-year", data: { stripe: "exp-year" } %>
  ...other fields and submit...
<% end %>

# Split single expiration field into month/year
expiration = $("#stripe-card-expiry").payment("cardExpiryVal") # => {month: "value", year: "value"}
$("#stripe-card-exp-month").val(expiration.month || 0)
$("#stripe-card-exp-year").val(expiration.year || 0)

# Submit to Stripe.com and get token
Stripe.card.createToken($("#form-id"), handleStripeResponse)

根据您使用的 Stripe.js 版本,如果您使用“data-stripe...”项,Stripe.js 将自动从隐藏字段中获取值。

于 2014-01-18T00:13:05.847 回答