Asp.net中RadioButtonList的验证,具有从数据库中获取的JavaScript和值

问题描述 投票:0回答:1

我想在RadioButtonList上使用JavaScript验证,其中值由现有表#tblGender绑定,其中给定三个值Male,Female和Others。我想表明,如果未选中任何按钮,则返回错误“请选择至少一个按钮”。

asp.net database radiobuttonlist
1个回答
0
投票

使用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')
}
© www.soinside.com 2019 - 2024. All rights reserved.