0

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

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

1 回答 1

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;
}
于 2013-10-08T23:21:27.133 回答