[英]Button in Form tag will not submit or respond to Javascript/Jquery
我正在嘗試提交一個使用條帶的表單(帶有“提交”類型的按鈕標簽),我做了一些調試(嘗試),我知道我的表單中的其他元素正在工作,否則我將無法使用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,或者我的代碼可能是錯的,但我做了三件事來幫助這種情況:
如果有人對此有實際的解釋,那就太棒了,但我設法解決了這個問題並且實際上正在記錄提交。 希望如果有其他人遇到這個問題,他們可以提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.