条纹支付-Javascript没有插入输入
stripe payments - Javascript not inserting input
我一直在尝试使用stripe接受付款,并试图根据我找到的指南为其制作一个粗略的原型,但我似乎无法使其工作。名为"stripeToken"的新输入从不在提交之后插入。这导致我的PHP脚本永远无法执行。我试图理解为什么它从不插入。以下是脚本:
Javascript:(在页首)
<script src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('mykeyishere');
</script>
<script>
// Event Listeners
$('#payment-form').on('submit', generateToken);
var generateToken = function (e) {
var form = $(this);
// No pressing the buy now button more than once
form.find('button').prop('disabled', true);
// Create the token, based on the form object
Stripe.create(form, stripeResponseHandler);
// Prevent the form from submitting
e.preventDefault();
};
</script>
HTML/Javascript:(在头部和表单中都尝试过JS)
<form action="index.php" method="POST" id="payment-form">
<script>
var stripeResponseHandler = function (status, response) {
var form = $('#payment-form');
// Any validation errors?
if (response.error) {
// Show the user what they did wrong
form.find('.payment-errors').text(response.error.message);
// Make the submit clickable again
form.find('button').prop('disabled', false);
} else {
// Otherwise, we're good to go! Submit the form.
// Insert the unique token into the form
$('<input>', {
'type': 'hidden',
'name': 'stripeToken',
'value': response.id
}).appendTo(form);
// Call the native submit method on the form
// to keep the submission from being canceled
form.get(0).submit();
}
};</script>
<span class="payment-errors"></span>
<div class="row">
<label>
<span>Card Number</span>
<input type="text" data-stripe="number">
</label>
</div>
<div class="row">
<label>
<span>CVC</span>
<input type="text" data-stripe="cvc">
</label>
</div>
<div class="row">
<label>
<span>Expiration (MM/YYYY)</span>
<input type="text" data-stripe="exp-month">
</label>
<input type="text" data-stripe="exp-year">
</div>
<button type="submit">Submit</button>
</form>
您应该从表单中删除该脚本标记,并将其放在另一个脚本标记旁边。
也可以尝试在文档中包装您的事件绑定。准备
$(document).ready(function(){
$('#payment-form').on('submit', generateToken);
var stripeResponseHandler = function (status, response) {
var form = $('#payment-form');
// Any validation errors?
if (response.error) {
// Show the user what they did wrong
form.find('.payment-errors').text(response.error.message);
// Make the submit clickable again
form.find('button').prop('disabled', false);
} else {
// Otherwise, we're good to go! Submit the form.
// Insert the unique token into the form
$('<input>', {
'type': 'hidden',
'name': 'stripeToken',
'value': response.id
}).appendTo(form);
// Call the native submit method on the form
// to keep the submission from being canceled
form.get(0).submit();
}
};
var generateToken = function (e) {
var form = $(this);
// No pressing the buy now button more than once
form.find('button').prop('disabled', true);
// Create the token, based on the form object
Stripe.create(form, stripeResponseHandler);
// Prevent the form from submitting
e.preventDefault();
};
});
根据我的猜测(这不是一个好的猜测),#付款表单没有正确绑定,因为脚本在dom准备好之前就已经运行了?
还有一件事引起了我的注意。您有e.preventDefault(),它可以阻止表单的提交,但是您有一个response处理程序。是否调用了该响应处理程序?有没有什么请求出去条纹然后又回来了?
查看您的网络窗口,看看是否发生了这种情况。表单只能以.get(0).submit()的形式提交;响应处理程序的一部分,所以在stripe完成之后。
相关文章:
- 如何在dom元素中插入输入标记数据插件
- 将数据插入输入值-AngularJS
- 如果输入不存在,请插入输入
- 通过按钮将值插入输入标签
- 使用 Enter 提交表单会在插入输入字段后中断 - 原因以及如何修复
- 使用 jquery 插入“输入按键”
- 在段落中插入输入的表单输入文本
- 单击链接并将隐藏字段中的文本插入输入字段超过 1 次,然后替换输入字段中的文本
- 在“输入”上创建并插入输入
- 仅当关联复选框选中MySql时才插入输入文本数据
- 将字符插入输入文本框时的制表符光标
- 在函数中插入输入值作为参数不起作用
- 如何在表格单元格中插入输入字段
- 使用javascript向表单插入输入
- 用jquery按正确顺序动态插入输入
- 在表单中插入输入元素的属性值
- 动态插入输入框won'
- 如何在Twitter引导程序的弹出窗口中插入输入文本
- 条纹支付-Javascript没有插入输入
- JavaScript - 如何使用按钮将数字插入输入框