【问题标题】:Page not redirecting to stripe checkout after it passed form validation页面通过表单验证后未重定向到条带结帐
【发布时间】:2020-03-14 23:07:26
【问题描述】:

我正在使用 HTML 5 required 属性进行表单验证。现在我想要的是,如果表单已通过 HTML 5 验证,它应该将用户带到条带结帐(我故意在下面的代码中为 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>

【问题讨论】:

  • 脚本加载后,您正在检查表单的有效性。我的猜测是表单在这一点上是无效的,而你从未真正设置过事件监听器。
  • @mlibby 你能给我看示例代码吗,这样我就可以明白你的意思并标记你的答案:)

标签: javascript html


【解决方案1】:

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

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

...

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

参考文献

【讨论】:

    【解决方案2】:

    现在,如果表单在页面加载时有效,您的代码只会附加 click 侦听器。在没有其他表单行为的情况下,单击表单中的按钮会将其提交回它来自的页面,这就是它看起来像页面刷新的原因。

    您需要像这样在按钮单击事件处理程序中移动表单检查:

    <script>
        (function() {
            var stripe = Stripe('pk_test_xxx');
            var checkoutButton = document.getElementById('checkout-button-sku_xxx');
    
            checkoutButton.addEventListener('click', function() {
                if($('#tcform')[0].checkValidity()){
    
                    // When the customer clicks on the button, redirect
                    // them to Checkout.
                    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 + '//www.xxx.com',
                        cancelUrl: window.location.protocol + '//www.xxx.com',
                    })
                    .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;
                        }
                    });
                }
            });
      })();
    

    但听表单的submit 事件(https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event)可能会更好:

    $('#tcform').on('submit', function() {
         stripe.redirectToCheckout({ 
             ...
         });
    });
    

    submit 事件仅在表单验证后才会发出。

    【讨论】:

    • 它似乎不起作用。我已经尝试了您的解决方案和许多其他方法。让我更新我的代码
    • 我现在将其更改为通过按钮尝试,但看不到我做错了什么。由于我缺乏理解,也许这是同样的问题。我不是全职开发人员,只是为一个简单的网页写一点代码:)
    【解决方案3】:

    As Jannes Botis said,你可以使用preventDefault。您也可以从 submit 事件的 eventHandler 上返回 false,这也将取消表单提交。参考见:https://www.geeksforgeeks.org/when-to-use-preventdefault-vs-return-false-in-javascript/

    【讨论】:

      【解决方案4】:

      这也发生在我们身上。

      我们已将novalidate attribute(删除 HTML5 验证)添加到表单并仅通过 javascript 对其进行验证。

          <form id="tcform" novalidate>
          ...
          </form>
      

      【讨论】:

        猜你喜欢
        • 2017-07-29
        • 2021-03-08
        • 2022-09-29
        • 1970-01-01
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 2013-10-17
        相关资源
        最近更新 更多