我正在尝试生成警告消息。当单选按钮未选中时。 这是我的代码。
<form method=post name="spendform" id="spendform">
<input name="price" type="radio" class="invest-coin-check" id="pricemethod" >
<button type=submit class="btn btn-lg btn-p-h btn-bw" id="mypayment">Make Investmen
t 这是java脚本代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#mypayment').click(function () {
if ($('#pricemethod').val() == '') {
alert('Please Select Payment options');
return false;
}
});
});
</script>
最佳答案
使用 .is(':checked')
而不是 .val()
。 .is(':checked')
如果被选中则返回true
,否则返回false
。
$(document).ready(function() {
$('#mypayment').click(function(){
if(!$('#pricemethod').is(':checked')){
alert('Please Select Payment options');
return false;
}
});
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<form method=post name="spendform" id="spendform">
<input name="price" type="radio" class="invest-coin-check" id="pricemethod" >
<button type=submit class="btn btn-lg btn-p-h btn-bw" id="mypayment">Make Investmen</button>
</form>
关于javascript - 单选按钮的警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186798/