我有以下的HTML代碼需要單選按鈕,HTML和JavaScript
<input type="radio" name="months" value="TBI-6">
<input type="radio" name="months" value="UNI-6">
,我有這個submitt按鈕
$('#button-confirm').on('click', function() {
$.ajax({
type: 'post',
url: 'index.php?route=payment/zero_rate/confirm',
cache: false,
data:{
egn: $('#input-payment-egn').val(),
months: $('input[name=months]:checked').parent().text() + $('input[name=months]:checked').val()
},
beforeSend: function() {
$('#button-confirm').button('loading');
},
complete: function() {
$('#button-confirm').button('reset');
},
success: function() {
location = '<?php echo $continue; ?>';
}
});
});
我怎樣才能驗證此兩個單選按鈕?我不想讓用戶提交表單並離開頁面而不選擇單選按鈕之一?
瞭解'單選按鈕的preventDefault()'函數https://api.jquery.com/event.preventdefault/ – Grasper