我正在尝试提交一个使用条带的表单(按钮标签的类型为“提交”),我做了一些调试(尝试),我知道我的表单中的其他元素正在工作,否则我将无法使用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>
我不确定是不是因为在整个项目中,我混合了jQuery,或者我的代码可能是错的,但我做了三件事来帮助这种情况:
如果有人对此有实际的解释,那就太棒了,但我设法解决了这个问题并且实际上正在记录提交。希望如果有其他人遇到这个问题,他们可以提供帮助。