我想在RadioButtonList上使用JavaScript验证,其中值由现有表#tblGender绑定,其中给定三个值Male,Female和Others。我想表明,如果未选中任何按钮,则返回错误“请选择至少一个按钮”。
使用Javascript,您可以像这样:
function IsRadioButtonSelected() {
var arrRadio = document.getElementsByName("NameOfRadioButton"),
c = 0;
return arrRadio.forEach(function(item, index) {
item.checked && c++
}), c > 0
}
并这样称呼它:
if(IsRadioButtonSelected()){
alert('Radio Button is Selected')
}
else{
alert('Radio Button is not Selected')
}
...但是,如果您使用jQuery,它可能会很简单:
if($("input[name=NameOfRadioButton]:checked").length>0){
alert('Radio Button is Selected')
}else{
alert('Radio Button is not Selected')
}