gpt4 book ai didi

javascript - 页面通过表单验证后未重定向到 strip 结帐

转载 作者:行者123 更新时间:2023-12-01 15:11:57 26 4
gpt4 key购买 nike

我正在使用 HTML 5 required 属性进行表单验证。现在我想要的是,如果表单已经通过了 HTML 5 验证,它应该将用户带到 strip 结帐(我故意在下面的代码中为 SO 问题 xxx 输出信息)。

现在如果表单没有通过验证,提交就不会处理,这很好。但是在我完成表格并提交表格后,它并没有将我带到条纹结帐页面,它只是刷新了页面。我究竟做错了什么?

<form id="tcform">

<p><b>Quantity:</b> 1</p>
<b class="price">Price:</b> <s>£xx</s> <span style="color: red;">£xx</span>
<button class="btn btn-default buynow" id="checkout-button-sku_xxx" role="link">
Buy Now
</button>
<p>
<i style="font-size:small">Limited time offer</i>
</p>

<p class="tcparagraph">
<input id="field_terms" type="checkbox" required name="terms"> I accept the <u><a href="Terms" id="tclink">Terms and Conditions</a></u></p>

<div id="error-message"></div>
</form>

<script>
(function() {

var stripe = Stripe('pk_test_xxx');

var checkoutButton = document.getElementById('checkout-button-sku_xxx');

checkoutButton.addEventListener('click', function() {
// When the customer clicks on the button, redirect
// them to Checkout.

const isFormValid = checkoutButton.form.checkValidity();

if(isFormValid==true) {

stripe.redirectToCheckout({
items: [{
sku: 'sku_xxx',
quantity: 1
}],

// Do not rely on the redirect to the successUrl for fulfilling
// purchases, customers may not always reach the success_url after
// a successful payment.
// Instead use one of the strategies described in
// https://stripe.com/docs/payments/checkout/fulfillment
successUrl: window.location.protocol + '//metis-online.com/courses/course-BRFAITC009-order-confirmation',
cancelUrl: window.location.protocol + '//metis-online.com/order-cancelled',
})
.then(function(result) {
if (result.error) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer.
var displayError = document.getElementById('error-message');
displayError.textContent = result.error.message;
}
});
}

});
})();
</script>

最佳答案

您想阻止 默认行为 提交表格。这样做:

checkoutButton.addEventListener('click', function(event) {

...

if(isFormValid==true) {
event.preventDefault();

引用
  • Prevent Default on Form Submit
  • Event.preventDefault()
  • 关于javascript - 页面通过表单验证后未重定向到 strip 结帐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60688151/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com