单选按钮的警报消息

时间:2019-07-24 15:47:17

标签: javascript jquery

我正在尝试生成警报消息。未选择单选按钮时。 这是我的代码。

<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>

2 个答案:

答案 0 :(得分:0)

这里:

$('#mypayment').click(function(){
   if($('#pricemethod').is(':checked')){
      alert('Please Select Payment options');
   }else{
      $('#spendform').submit();
   }
});
MH2K9

.is(':checked')

答案 1 :(得分:0)

使用.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>