Javascript单选按钮警报的问题

时间:2013-10-08 20:08:52

标签: javascript button alert radio

我需要一个警报,以便在没有选择任何单选按钮时显示。

Html代码:

<tr>
<td><input id="Radio1" name="delivery" value="M" type="radio">M</td>
</tr>
<tr>
<td><input id="Radio2" name="delivery" value="P" type="radio">P</td>
</tr>
<tr>
<td><input id="Radio3" name="delivery" value="S" type="radio">S</td>
</tr>

使用Javascript:

        deliveryChecked = ""
    for (i = 0; i < f.deliveryChecked; i++) {
        if (f.deliveryChecked[i].checked) {
            deliveryChecked = f.delivery[i].value
            break
        }
    }

          if (deliveryChecked == "") {
            alert("You did not make a delivery selection!")
        return false
    }  

1 个答案:

答案 0 :(得分:0)

HTML:

<form onsubmit="return validate(this)">
    <input id="Radio1" name="delivery" value="M" type="radio" />
    <input id="Radio2" name="delivery" value="P" type="radio" />
    <input id="Radio3" name="delivery" value="S" type="radio" />
    <input type="submit" />
</form>

JS:

function validate(frm){
    var atLeastOneChecked=false;
    [].forEach.call(frm.delivery,function(input){
        atLeastOneChecked=atLeastOneChecked?
            true
                :
            input.checked;
    });
    if(!atLeastOneChecked){
        alert("You did not make a delivery selection!");
        return false;
    }
    return true;
}