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

问题描述 投票:0回答:1

我正在尝试提交一个使用条带的表单(按钮标签的类型为“提交”),我做了一些调试(尝试),我知道我的表单中的其他元素正在工作,否则我将无法使用javascript(card.mount)查看我已挂载到表单中的输入。按钮实际上是该表单中唯一不能以任何方式使用的元素,也不会提交。

无论我使用什么方法,无论是jquery还是普通的vanilla javascript,我都无法通过此按钮来响应我指定的任何事件(点击,我希望它提交表单)。它拒绝听。

<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>
javascript php jquery html laravel
1个回答
0
投票

我不确定是不是因为在整个项目中,我混合了jQuery,或者我的代码可能是错的,但我做了三件事来帮助这种情况:

  1. 将所有javascript包含在$(document).on('ready',function(){})和。
  2. 从我的button元素中删除了该类的名称,类型和几乎所有内容。
  3. 而不是将表单id放入变量,我使用jQuery直接分配表单。例如:$('#payment-form')。on('submit',function(){}),我必须这样做,因为当我独自使用变量而不管其他所有东西时,它仍然没有'工作。

如果有人对此有实际的解释,那就太棒了,但我设法解决了这个问题并且实际上正在记录提交。希望如果有其他人遇到这个问题,他们可以提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.