duandai3964 2019-03-29 04:40
浏览 75

表单标签中的按钮不会提交或响应Javascript / Jquery

I'm trying to submit a form for using stripe (button tag with a type of "submit") and I did some debugging (attempted), I know that the other elements within my form are working or else I would not be able to see the input that i've mounted into my form using javascript (card.mount). The button is literally the only element within that form that I can not utilize in any way, nor will it submit.

No matter what method I use, whether it be jquery or just regular vanilla javascript, I can not get this button to react to any events that I assign i (on click, i want it to submit the form). It refuses to listen.

<form action="{{ route('checkout') }}" method="post" id="payment-form">
    <div class="form-row">
        <label for="card-element">
            Credit or debit card
        </label><hr>
        <div id="card-element">
            <!-- A Stripe Element will be inserted here. -->
        </div>

        <!-- Used to display Element errors. -->
        <div id="card-errors" role="alert"></div>
    </div>
    {{ csrf_field() }}
    <button type="submit" id="button2" class="btn btn-success">Buy Now</button>
</form>

<script>
//in my javascript file

// Custom styling can be passed to options when creating an Element.
    var style = {
        base: {
            color: '#32325d',
            fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
            fontSmoothing: 'antialiased',
            fontSize: '16px',
            '::placeholder': {
                color: '#aab7c4'
            }
        },
        invalid: {
            color: '#fa755a',
            iconColor: '#fa755a'
        }
    };

// Create an instance of the card Element.
    var card = elements.create('card', {
        style: style
    });

// Add an instance of the card Element into the `card-element` <div>.
    card.mount('#card-element'); //this portion works otherwise I would not be able to type anything within the form. and I can use javascript to manipulate it,

    var form = document.getElementById('payment-form');
    form.addEventListener('submit', function (event) {
        event.preventDefault();
        // here is the problem, I can't get a submit because the button I have in the form refuses to actually submit anything. It justs works like a regular button and randomly submits, ignoring the fact that it is inside the form

        stripe.createToken(card).then(function (result) {
            if (result.error) {
                // Inform the customer that there was an error.
                var errorElement = document.getElementById('card-errors');
                errorElement.textContent = result.error.message;
            } else {
                // Send the token to your server.
                stripeTokenHandler(result.token);
            }
        });
    });
</script>
  • 写回答

1条回答 默认 最新

  • douqiaosu0280 2019-03-29 08:16
    关注

    I am not sure if it's because throughout the entirety of the project, I have a mix of both jQuery, or maybe my code was just wrong, but I did three things to aid this situation:

    1. wrapped all of my javascript within $(document).on('ready', function(){}) and.
    2. removed the name,type, and virtually anything besides the class from my button element.
    3. rather than putting the form id into a variable, I used jQuery to assign the form directly. ex: $('#payment-form').on('submit', function(){}), I had to do this because when I left it alone using the variable regardless of all that other stuff, it still didn't work.

    If anyone has an actual explanation for that, that'd be wonderful, but I managed to fix this issue and the submit is actually being recorded. Hopefully if anyone else runs into this issue, they can aid that.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题