我有以下html代码
<input type="radio" name="months" value="TBI-6">
<input type="radio" name="months" value="UNI-6">我有一个提交按钮
$('#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; ?>';
}
});
});如何验证这两个单选按钮?我不想让用户提交表单并离开页面,而不选择其中一个单选按钮?
发布于 2017-09-27 02:58:39
$('input[name=months]:checked').length 应该告诉你在组中有多少个单选按钮被选中了。
因此,在继续提交表单之前,请验证它是否高于0。
发布于 2017-09-27 03:03:05
如果选中,请选中单选按钮
<input type="radio" name="months" value="TBI-6" class="radiobtn">
<button id="button-confirm">
Submit
</button>
$('#button-confirm').on('click', function() {
if($('.radiobtn').is(':checked')) { alert("it's checked");
$.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; ?>';
}
});
} else {
alert("it's not checked");
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="months" value="TBI-6" class="radiobtn">
<button id="button-confirm">
Submit
</button>
https://stackoverflow.com/questions/46433799
复制相似问题